Reputation: 3690
I have two tables called Siteaddress
and Jobsheet
. In the Jobsheet
table there is a field called siteaddress
that contains the ID of the site address stored in the Siteaddress
table.
The problem I am having is that while I'm doing the join in the controller, CakePHP is joining the Siteaddress
table to the Jobsheet
table using the id
fields in both tables. This causes a problem as there is only one record (so far) in the Siteaddress
table, and multiple records in the Jobsheet
table.
What I need to do is tell CakePHP to join Jobsheet.siteaddress
to Siteaddress.id
instead of the way it's working at the moment.
How am I able to do this?
Below is the relationship code from the Jobsheet
model file:
public $hasOne = array(
'Company' => array(
'className' => 'Company',
'foreignKey' => 'id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Siteaddress' => array(
'className' => 'Siteaddress',
'foreignKey' => 'id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Cheers!
Upvotes: 0
Views: 944
Reputation: 4411
You've set the foreignKey
to id
so Cake uses this for making the join. This key can be set to any table field you want in the table you want joined. If I understand you correctly you should set the foreignKey
to siteaddress
:
'Siteaddress' => array(
'className' => 'Siteaddress',
'foreignKey' => 'siteaddress',
'conditions' => '',
'fields' => '',
'order' => ''
)
See the manual entry for hasOne
as well.
EDIT:
In this case $belongsTo
might be a better fit as an association.
Upvotes: 1