I have created a table called Delegates
, and I created a UsersTable and User Entity. And I have used $this->setTable('delegates');
in UsersTable
to be able to access Delegates from $this->Users;
(I just want to say I have created a User Model with delegates table)
So far so good...
In my application I am trying to access deep associations. Every thing is fine with this query but when I contain
the User
model I get The Users association is not defined on Comments.
I can confirm the associations are set correctly.
...
// There are more associations up there.
...
'Comments', [
'className' => 'App\Model\Table\CommentsTable',
'foreignKey' => 'delegate_assessment_criteria_id',
'belongsTo' => [
'Assessors' => [
'className' => 'App\Model\Table\AssessorsTable',
'foreignKey' => 'assessor_id',
],
'Users' => [
'className' => 'App\Model\Table\UsersTable',
'foreignKey' => 'delegate_id',
]
]
]
Here is deep association.
...
// There are more associations up there.
...
'Comments' => function($q) {
return $q
->select([
'id'
])
->order(['Comments.created DESC'])
->contain([
'Assessors' => function($q) {
return $q
->select([
'id'
])
->enableAutoFields();
}
])
->contain([
'Users' => function($q) {
return $q
->select([
'id',
'delegate_id'
])
->enableAutoFields();
}
])
->enableAutoFields();
}
2 Notes:
- If I contain the
User
model in very first in hierarchy of my query I can access theUser
fields but in deep association this doesn't work. - If I contain
Delegates
it works.
I believe there is a problem with Cakephp query builder.
Users
andDelegates
. I want to usedelegates
asUsers
. All the associations has been setup in (Table Models
). Please read the question again. I believe there is problem with deep ORM associations. – Fury