在Cakephp中使用不等于运算符

时间:2022-12-17 11:47:47

I have find query that looks like this.

我找到了这样的查询。

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

where $blocked_ids is an array of ids. It throws an error

其中$ 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')

When I removed != it works fine. No Errors

当我删除!=它工作正常。没有错误

I appreciate any help.

我感谢任何帮助。

4 个解决方案

#1


22  

As @Josh suggested, try using NOT IN. The correct format for CakePHP 1.3.x is:

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

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

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

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

#2


10  

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

#3


2  

This should be handled when you're getting your data in your model function. You should exclude them by using code like this:

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

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

#4


0  

Try using NOT IN instead [MySQL Reference]:

尝试使用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']) . "%"))

#1


22  

As @Josh suggested, try using NOT IN. The correct format for CakePHP 1.3.x is:

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

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

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

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

#2


10  

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

#3


2  

This should be handled when you're getting your data in your model function. You should exclude them by using code like this:

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

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

#4


0  

Try using NOT IN instead [MySQL Reference]:

尝试使用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']) . "%"))