在Cakephp中使用不等于运算符

时间:2011-12-01 11:51:14

标签: php cakephp cakephp-1.3

我找到了这样的查询。

$this->paginate('Article', array('Article.status !=' => 'Inactive', 'Article.user_id !=' => $blocked_ids, 'Article.tags LIKE' => "%" . trim($this->params['url']['tag']) . "%"))

其中$ blocked_ids是一个id数组。它会抛出错误

SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= ('170')

当我删除!=时,它运行正常。没有错误

我感谢任何帮助。

4 个答案:

答案 0 :(得分:23)

正如@Josh建议的那样,尝试使用NOT IN。 CakePHP 1.3.x的正确格式是:

$this->User->find('all', array(
    'conditions' => array(
        'NOT' => array(
            'User.id' => array(1, 2, 3)
        )
    )
));

取自http://cakebaker.42dh.com/2007/04/26/how-to-use-not-in-in-a-condition/

答案 1 :(得分:11)

$this->paginate(
                'Article',
                array (
                     'Article.status <>' => 'Inactive', 
                     'Article.user_id !=' => $blocked_ids, 
                     'Article.tags LIKE' => "%" . trim($this->params['url']['tag']) . "%"
                   )
              );

答案 2 :(得分:2)

当您在模型函数中获取数据时,应该处理此问题。您应该使用以下代码排除它们:

'conditions' => array(
     'Article.status' => 'active',

答案 3 :(得分:0)

尝试使用NOT IN代替 [MySQL Reference]

$this->paginate('Article', array('Article.status !=' => 'Inactive', 'Article.user_id NOT IN' => $blocked_ids, 'Article.tags LIKE' => "%" . trim($this->params['url']['tag']) . "%"))