简体   繁体   中英

Cakephp model find multiple 'not like'

A VARCHAR field in my db has a comma separated list of tags.

I have a list of values that I want to exclude, but having trouble.

This is what I was trying...

$result = $this->Tags->find(
  'all',
  'conditions'=>array(
      'fieldName NOT LIKE'=>'%tag1%',
      'fieldName NOT LIKE'=>'%tag2%'
  )
);

That works on the first line in the condition only.

Have also tried...

$result = $this->Tags->find(
  'all',
  'conditions'=>array(
      'NOT'=>array(
          'fieldName LIKE'=>'%tag1%',
          'fieldName LIKE'=>'%tag2%'
      (
  )
);

I keep looking in the Cake documentation, but haven't been able to find this specifically. Thanks.

A few issues here. First of all, the syntax is incorrect. I assume that this is just a mistake you made typing your question.

The second issue is harder to detect. If you include in the array the key

'fieldName NOT LIKE' 

more than once, it will overwrite itself.

To prevent this, you have to wrap it in an array:

$result = $this->Tags->find('all',array(
    'conditions'=>array(
        array('fieldName NOT LIKE'=>'%tag1%'),
        array('fieldName NOT LIKE'=>'%tag2%')
)));

Another option is to write the query as follows:

$result = $this->Tags->find('all',array(
    'conditions'=>array(
        'fieldName NOT REGEXP'=>'tag1|tag2',
)));

i look your syntax is not true, maybe you forget array after 'all',

like this,

$result = $this->Tags->find('all',array(
    'conditions'=> array( 'fieldName NOT LIKE' =>'%tag1%',
    'fieldName NOT LIKE'=>'%tag2%')
 ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM