LoginSignup
26
27

More than 5 years have passed since last update.

find で複数の or を and でつなげる連想配列

Last updated at Posted at 2012-04-23

頭の中が SQL モードになっている場合によくミスる

こういうSQL文を発行したい場合

SELECT * FROM users 
WHERE (users.name = 'hoge' OR users.age = 20) 
AND (users.name = 'foo' OR users.age = 30);

find の引数となる連想配列は次のようになる

// 正しい連想配列
$conditions = array(
  array( 'or' => array('name'=>'hoge', 'age'=>'20')),
  array( 'or' => array('name'=>'foo', 'age'=>'30')),
);

連想配列の仕組み上、下のような連想配列では、キー:ORが上書きされる。
脳内がSQL モードになっていると意外に気が付かない。

// 誤った連想配列
$conditions = array(
  'or' => array('name'=>'hoge', 'age'=>'20'),
  'or' => array('name'=>'foo', 'age'=>'30'),
);
26
27
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
26
27