2013年7月24日水曜日

CakePHPでの検索条件例集

CakePHPでの検索条件例集

condition

OR, プレースホルダ
$conditions = array(
'OR' => array(
            array('startDate > date(?)' => array($today)),
            array('endDate < date(?)' => array($today)),
        )
);
array('Post.read_count BETWEEN ? AND ?' => array(1,10))
大小
$conditions = array(
        'Task.date >=' => $start,
        'Task.date <=' => $end
);
否定
array("Post.title !=" => "This is a post")
IN(…)、 NOT IN
array(
    "Post.title" => array("First post", "Second post", "Third post")
)
array(
    "NOT" => array("Post.title" => array("First post", "Second post", "Third post"))
)
比較
array("Post.created = Post.modified")
LIKE
array(
    "Author.name" => "Bob",
    "OR" => array(
        "Post.title LIKE" => "%magic%",
        "Post.created >" => date('Y-m-d', strtotime("-2 weeks"))
    )
)
 
データを取得する - CakePHP Cookbook v2.x documentation 
http://book.cakephp.org/2.0/ja/models/retrieving-your-data.html