Using not equal operator in Cakephp

Josh Randall picture Josh Randall · Dec 1, 2011 · Viewed 26.9k times · Source

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

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.

Answer

Nick Zinger picture Nick Zinger · Dec 1, 2011

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

$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/