Reputation: 198
I am using CakePhp 3.x and i am trying to filter data on the bases of associated data. Let suppose i have two models Users and Profiles
Users Has-one Profile and Profiles belongs to a user.
so i want to get those users who haven't profile so kindly help me how can i apply the conditions ?
$this->Users->find('all',[
'conditions'=>['Profile.id'=>Null]
])
I was trying like this i thing it is wrong and then i tried it with contain but contain makes filter on associated data not to the users so is there any way to get those user who have no profile ?
Upvotes: 1
Views: 51
Reputation: 198
See the Link below :-
http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#using-notmatching
There are three type of filtration of data on the bases of associated data.
1 -> matching
2 -> noMatching
3 -> See the Link above
Matching This function will filters results that have relation to the specified association: for now it's profiles.
$this->Users->find()->matching('Profiles') //This will get those user how have profile
NoMatching
The opposite of matching() is notMatching(). This function will change the query so that it filters results that have no relation to the specified association:
$this->Users->find()->noMatching('Profiles') //This will get those user how have no profile
Upvotes: 1
Reputation: 8618
You could try something like this:
$query = $this->Users->find()
->contain([
'Profiles' => function ($q) {
return $q
->select(['id', 'name']) // Your profile fields, say id, name etc.
->where(['Profiles.id' => NULL]);
}
]);
Hope this helps.
Upvotes: 0