【问题标题】:Using not equal operator in Cakephp在 Cakephp 中使用不等于运算符
【发布时间】:2012-01-10 13:43:23
【问题描述】:

我找到了类似这样的查询。

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

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

感谢您的帮助。

【问题讨论】:

    标签: php cakephp cakephp-1.3


    【解决方案1】:

    正如@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/

    【讨论】:

      【解决方案2】:
      $this->paginate(
                      'Article',
                      array (
                           'Article.status <>' => 'Inactive', 
                           'Article.user_id !=' => $blocked_ids, 
                           'Article.tags LIKE' => "%" . trim($this->params['url']['tag']) . "%"
                         )
                    );
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 2019-03-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-05-04
            • 2012-09-03
            • 2016-07-29
            • 2014-03-22
            • 2021-12-29
            相关资源
            最近更新 更多