2
2

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 5 years have passed since last update.

CakePHP 3で複数のOR条件をANDで連結する方法

Last updated at Posted at 2018-04-25

要件

こういうSQLを発行したい場合の方法。

SELECT id, name, age
FROM users
WHERE (name LIKE 'さとう%' OR kana = 'さとう%')
AND (stateA = 'a' OR stateB = 'b');

$paginateを使う例

// 1つ目のOR条件
$this->paginate['conditions'][]['or'] = [
    'name LIKE' => '%さとう',
    'kana LIKE' => '%さとう'
];
// 2つ目のOR条件
$this->paginate['conditions'][]['or'] = [
    'stateA' => 'a',
    'stateB' => 'b'
];

$queryを使う例

// 1つ目のOR条件
$query->where([
    0 => [
        'or' => [
            'name LIKE' => '%さとう',
            'kana LIKE' => '%さとう'
        ]
    ],
    1 => [
        'or' => [
            'stateA' => 'a',
            'stateB' => 'b'
        ]
    ]
]);
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?