Using not equal operator in Cakephp
-
26-10-2019 - |
Question
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.
Solution
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/
OTHER TIPS
$this->paginate(
'Article',
array (
'Article.status <>' => 'Inactive',
'Article.user_id !=' => $blocked_ids,
'Article.tags LIKE' => "%" . trim($this->params['url']['tag']) . "%"
)
);
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',
Try using NOT IN
instead [MySQL Reference]:
$this->paginate('Article', array('Article.status !=' => 'Inactive', 'Article.user_id NOT IN' => $blocked_ids, 'Article.tags LIKE' => "%" . trim($this->params['url']['tag']) . "%"))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow