The cakephp $ mapping belongs to a non-primary key association

I was told that to match $ attribTo for a non-primary key, I would set foreignKey to false and set the conditions by someone in another forum (actually IRC). However, I do not think that I am doing it right. Below is my code that I used.

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

When I look at the generated SQL query, the ON clause in the JOIN looks for a string value instead of a column: `RentalLineitem`.` i_num` = 'Inventory.i_num' instead of what I need, it is` RentalLineitem`.` i_num` = `Inventory `.` i_num`.

I was told to change the correctness of changing the database schema. However, this is an outdated application, the database has been around for 10 years, and there are other applications that use this database. I need to work with the tables that I have, and I cannot change the layout.

How can I correctly link these models?

+6
source share
2 answers

hmm this might not be the case, but I already had some similar problems, and I fixed it by doing something like:

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

hope this helps

Luck

+11
source

You simply edit in such conditions:

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

It works correctly in my case.

0
source

Source: https://habr.com/ru/post/889969/


All Articles