如何在find()cakephp中指定范围

时间:2012-12-05 13:52:18

标签: mysql cakephp range

我是CakePHP的新手,我尝试过搜索,但我找不到这个问题的答案。

简单地说,我希望查询是这样的:

SELECT id from posts WHERE id IN (15,18,20);

但我不知道在find()电话中放什么。

4 个答案:

答案 0 :(得分:2)

CakePHP在线手册http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions对此进行了介绍。只需在conditions中指定一个数组:

<?php
$ids = array(1,2,3,4,5,6);
$results = $this->Post->find('all', array(
    'conditions' => array(
        'Post.id' => $ids
    )
));

答案 1 :(得分:1)

从模型中可以看出:

$ids   = array(15, 18, 20);
$posts = $this->find('all', array(
    'conditions'  => array(
        'Post.id' => $ids
     )
);
在条件数组中,您可以传递要在“IN”子句中使用的值数组

答案 2 :(得分:-1)

来自帖子控制器

$id_array = array(15, 18, 20);
$this->Post->find('all', array('Post.id' => $id_array));

有关此主题的更多信息 http://book.cakephp.org/2.0/en/models/retrieving-your-data.html

答案 3 :(得分:-1)

$conditions = array("Post.title" => array("First post", "Second post", "Third post")) $this->find(all,array($conditions));

如果它有用的话,请点击它。