php 在 Cakephp 中使用不等于运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8340901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:28:27  来源:igfitidea点击:

Using not equal operator in Cakephp

phpcakephpcakephp-1.3

提问by Josh Randall

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.

我很感激任何帮助。

回答by Nick Zinger

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/

回答by Rumi

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

回答by jwg2s

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',

回答by Josh

Try using NOT INinstead[MySQL Reference]:

尝试NOT IN改用[MySQL 参考]

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