Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthorhpatoio
     

    I'm trying to setup a habtm for a DB that already exists, so tables are named differently. but it doesn't works.

    In the documention I read

    join_table - specify the name of the join table if the default based on lexical order isn't what you want. WARNING: If you're overwriting the table name of either class, the table_name method MUST be declared underneath any $has_and_belongs_to_many declaration in order to work.

    What does it means ? Where is my code wrong (look after) ?


    I get this message (user_id doesn't exists, that's why I set 'foreing_key' => 'MY_OLD_KEY'):

    Fatal error: Opps! We could not find primary key column user_id on the table MY_OLD_RELATION_TABLE, for the model UserCustomer in /usr/home/my_user/akelos/lib/AkActiveRecord.php on line 2404

    Here my code:

    class User extends ActiveRecord { var $habtm = array ( 'customers' => array( 'join_table' => 'MY_OLD_RELATION_TABLE', 'foreing_key' => 'MY_OLD_KEY', 'association_foreing_key' => 'MY_OLD_KEY_2' ) ); function __construct() { $this->setTableName('MY_OLD_TABLE'); $this->setPrimaryKey('MY_OLD_KEY'); $this->init(func_get_args()); } [other methods] }

    -- Simone

    • CommentAuthorKaste
     

    more info? test-case?

    Users table-name is MY_OLD_TABLE, its primary-key is MY_OLD_KEY. UserCustomers table is MY_OLD_RELATION_TABLE and also has a column MY_OLD_KEY?

    what is the content of the UserCustomer-Model?

    • CommentAuthorhpatoio
     

    Yes, tables are like that. Here the structure (changed a bit the names so they are more clear).

    MY_USER

    MY_USERKEY field_a1 field_a2 field_a3

    MY_CUSTOMER

    MY_CUSTOMERKEY field_b1 field_b2 field_b3

    MY_RELATION_TABLE

    MY_USERKEY MY_CUSTOMERKEY

    The key for MY_RELATION_TABLE is (MY_USERKEY + MY_CUSTOMERKEY). Question: Do I need a ID field to be set as a key ?

    Here is my model

    (The model for the table MY_CUSTOMER is called "customer" and the model for the table MY_USER is named "user". Honestly I don't understand why Akelos user the table name for user and the model name for customer)

    <?php class MY_USERCustomer extends ActiveRecord { var $_avoidTableNameValidation = true; function MY_USERCustomer() { $this->setModelName("MY_USERCustomer"); $attributes = (array)func_get_args(); $this->setTableName('MY_RELATION_TABLE', true, true); $this->init($attributes); } } ?>

    When I visit the page I get :

    Fatal error: Opps! We could not find primary key column user_id on the table MY_RELATION_TABLE, for the model MY_USERCustomer in /usr/home/myuser/akelos/lib/AkActiveRecord.php on line 2404

    And actually is right because the field user_id doesn't not exists. (even if I've specified the right name for the key in the model for User) I don't understand why this happen. Shouldn't Akelos use as name the one specified in the habtm relation ?

    Is it mandatory to has a single key field for the relation table ? (as you can see the key for MY_RELATION_TABLE is made with 2 fields)

    thanks

    -- Simone

    Here

    • CommentAuthorKaste
     

    #

    class User...
    var $habtm = array (
        'customers' => array(
            'class_name'=>'Customer',
            'table_name'=>'MY_CUSTOMER',
            'join_table' => 'MY_OLD_RELATION_TABLE',
            'foreing_key' => 'MY_OLD_KEY',
            'association_foreing_key' => 'MY_OLD_KEY_2'
        )
    );
    
    • CommentAuthorhpatoio
     

    No bug in Akelos, it was my fault !!

    I wrote foreing_key and association_foreing_key instead of foreign_key and association_foreign_key :(

    So, foreING instead of foreIGN !!

    That's why Akelos didn't map the table/key correctly !

    Thank anyway Kaste !

    -- Simone

    • CommentAuthorKaste
     

    current implementation needs a unique, primary-key (id) in the join-table. that will lead to a problem on your legacy-db, I guess.