0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CakePHP3のfindでcontain要素に検索条件を追加する

Posted at

例えばUsersテーブルにUserInfoテーブルが紐付いていて、prefecture_id=2で名字が佐藤か工藤の1名をFindしたいときは下記のようになります。

useで$prefecture_idをわたしてあげるのがミソです。

UsersController.php
$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' => ['佐藤', '工藤']
    ])
    ->first();

かんたんですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?