Issue
Sample query:
TableRegistry::getTableLocator()
->get('Parents')
->find()
->contain([
'Children' => function (Query $query) {
return $query->where([
'Children.code = Parent.code'
]);
}
])
Parent
and Children
tables only have code
as common field.
How do I define their association?
How do I contain unassociated entities?
Solution
You can customize the foreignKey
and bindingKey
in association configuration
In ParentsTable.php:
$this->hasMany('Children', [
'bindingKey' => 'code',
'foreignKey' => 'code'
]);
This config will set which fields to look for when associating entities.
Then you could associate entities on your controller like this:
// This query will contain children where Children.code === Parent.code
TableRegistry::getTableLocator()
->get('Parents')
->find()
->contain('Children');
Answered By - kgbph
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.