Add search criteria to contain element in CakePHP3 find
Sep 8, 2020
PHP
MySQL
cakephp3
For example, if the UserInfo table is linked to the Users table and you want to find one person whose surname is Sato or Kudo with prefecture_id = 2, it will be as follows.
It is a miso to give you $ prefecture_id with use.
$ prefecture_id = 2;
$ user = $ this-> Users-> find ()-> contain (
['UserInfo' => function ($ q) use ($ prefecture_id) {
return $ q-> where (['UserInfo.prefecture_id' => $ prefecture_id]);
}])
-> where ([[
'Users.last_name IN'=> ['Sato','Kudo']
])
-> first ();
It’s easy.