Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp mapping $belongsTo association to a non primary key

I was told that to accomplish mapping the $belongsTo to a non primary key, I would set the foreignKey to false and set the conditions by someone on another forum (IRC, actually). However, I don't think I'm doing this correctly. Below is my $belongsTo code I was trying:

var $belongsTo = array(

    'Inventory' => array(
        'className'    => 'Inventory',
        'foreignKey' => false,
        'conditions' => array('RentalLineitem.i_num' => 'Inventory.i_num'),
        'dependent'    => false
    )

);

When I look at the SQL query that's being generated, the ON clause in the JOIN is looking for the string value instead of the column: `RentalLineitem`.`i_num` = 'Inventory.i_num' instead of what I need which is `RentalLineitem`.`i_num` = `Inventory`.`i_num`.

I've been told to change the "just change the database schema" to be correct. However, this is a legacy application, the database already has existed for 10 years, and there are other applications using this database. I HAVE to work with the tables I have, and I can not change the schema.

How can I properly associate these models?

like image 657
stephenbayer Avatar asked Jun 07 '11 15:06

stephenbayer


1 Answers

hmm this might not be the correct way, but i already had some similar problems and i corrected it by doing something like:

'conditions' => array(' `RentalLineitem`.`i_num` = `Inventory`.`i_num`'),

hope this helps,

Good Luck

like image 157
pleasedontbelong Avatar answered Oct 23 '22 04:10

pleasedontbelong