How to set foreignKey for belongsTo cakephp without id
Cakephp has a good ways for handling table relationship model using AppModel association structure. It is easy to create and set relation between two or more tables on your database. Cakephp has setting for one to one, one to many, many to many relationship.
I would like to tell you about setting foreignKey for one to many relationship in cakephp that the master table doesn’t have foreignKey id. On your model, the relationship for one to many that set on child table would look like this
<?php class CurrencyRate extends AppModel { var $belongsTo = array('Currency' => array( 'className' => 'Currency', 'foreignKey' => 'currency_id', )); } ?>
How if CurrencyRate model don’t have currency_id to associate with Currency Table? For example we have code fields on CurrencyRate model, and then Currency model also have code fields. How do we set this relationship?
The solution is very easy by setting the foreignKey option into false, and then set conditions into ‘CurrencyRate.code = Currency.code’.
Final code would look like this
<?php class CurrencyRate extends AppModel { var $belongsTo = array('Currency' => array( 'className' => 'Currency', 'foreignKey' => false, 'conditions' => 'CurrencyRate.code = Currency.code', )); } ?>
Have you had this working in CakePHP 1.3? I am trying the following:
class User extends AppModel {
var $name = ‘User’;
var $belongsTo = array(
‘Profile’ => array(
‘foreignKey’ => false,
‘conditions’ => array(‘User.email’ => ‘Profile.user_email’)
)
);
}
But receiving an error when i perform a select query. It doesn’t seem to be joining the User model when querying the Profile model. The error reads: SQL Error: 1054: Unknown column ‘User.email’ in ‘where clause’
Any ideas?
Comment by Chris Hale — October 14, 2010 @ 10:54 am
I am using cake 1.3 also. Btw i was testing to set conditions using an array, the relationship seems does not work correctly. so I think you need to set the relation in plain string. Try this one
var $belongsTo = array(
‘Profile’ => array(
‘foreignKey’ => false,
‘conditions’ => ’User.email=Profile.user_email’
)
);
Comment by admin — October 14, 2010 @ 8:36 pm
Right! I figured out what it was. It was because i was searching from an associated model. I just assumed it would work but it doesnt.
I had a Product model that belonged to a User like so:
var $belongsTo = array(
‘User’ => array(
‘className’ => ‘User’,
‘foreignKey’ => false,
‘conditions’ => array(‘User.email = Product.user_email’)
)
);
and so i thought that if i associated the profile to the user like so:
var $belongsTo = array(
‘Profile’ => array(
‘foreignKey’ => false,
‘conditions’ => array(’User.email = Profile.user_email’)
)
);
Then if i performed a find on the Product model the Profile would be found through association on the User model. Where infact it seems to just be finding the first one in the database regardless of whether it means the conditions. In the end i just associated the Product and Profile model and queried it that way.
Hope this is of some use to some one.
Comment by Chris Hale — October 15, 2010 @ 4:09 am