简体   繁体   English

Cakephp模型找到多个“不喜欢”

[英]Cakephp model find multiple 'not like'

A VARCHAR field in my db has a comma separated list of tags. 我的数据库中的VARCHAR字段具有逗号分隔的标签列表。

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. 我一直在看Cake文档,但是还没有找到它。 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', 我看你的语法是不正确的,也许你忘了'all'之后的数组,

like this, 像这样,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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