Reputation: 1165
Basically, im trying to learn the basics of cakephp and im stuck at sorting the relationships out. I have tried loads of ways with no success.
I have a lead table which needs to connect to the contact table, there can be many leads to one contact. Im not sure how to do this, could someone please help?
lead model:
<?php
class Lead extends AppModel {
var $name = 'Lead';
var $belongsTo = array(
'Contact' => array(
'className' => 'Contact',
'foreignKey' => 'contact_id'
)
);
}
?>
contact
<?php
class Contact extends AppModel {
var $name = 'Contact';
var $hasMany = array(
'Lead' => array(
'className' => 'Lead',
'foreignKey' => 'contact_id'
)
);
}
?>
Upvotes: 4
Views: 103
Reputation: 1300
Since once contact can have many leads, you'll want to add
var $hasMany = 'Lead';
to your User class.
And since I assume that Lead has a foreign key referencing the 'owning' User, you'll want to add
var $belongsTo = 'User';
to your Lead class.
This will let you access leads and users from both sides of the relationship (from the lead perspective, and from the user perspective).
See the docs for hasMany and belongsTo for more info.
Upvotes: 1