CakePHP语法用于查找条件

时间:2013-01-21 14:58:45

标签: cakephp cakephp-2.1

我有一个名为Users的表,以下查找工作正常:

$users = $this->User->find('list', array('conditions'=>array('User.id'=>1)));

但是,如果我尝试获取"User.id != 1" or "User.id >= 5",的用户列表,则查找不会返回数据。

我使用的语法是

$users = $this->User->find('list', array('conditions'=>array('User.id'=>'<> 1')));

$users = $this->User->find('list', array('conditions'=>array('User.id'=>'>= 1')));

Cake为User.id => '<> 1')案例生成的调试查询是

SELECT `User`.`id`, `User`.`first_name` FROM `mesh2`.`users` AS `User` WHERE `User`.`id` = '<>1',

似乎不正确。

我使用了Alvin的site中的例子。有什么想法吗?

3 个答案:

答案 0 :(得分:8)

您需要将>=放在数组的正确一侧:

$this->User->find('list', array(
    'conditions'=>array('User.id >=' => 1)));

如“官方”文件中所述:

http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

答案 1 :(得分:0)

默认情况下可能的键,所有键都是可选的:

$params = 
        array(
        'conditions' => array('Model.field' => $thisValue), //array of conditions
        'recursive' => 1, //int
        //array of field names
        'fields' => array('Model.field1', 'DISTINCT Model.field2'),
        //string or array defining order
        'order' => array('Model.created', 'Model.field3 DESC'),
        'group' => array('Model.field'), //fields to GROUP BY
        'limit' => n, //int
        'page' => n, //int
        'offset' => n, //int
        'callbacks' => true //other possible values are false, 'before', 'after'
    );

查询:

$resp = find('all', $params);

debug($resp);

答案 2 :(得分:0)

$this->User->find('list', array(
    'conditions'=>array('User.id >=' => true)));