Symfony ArrayCollection上的条件表达式区分大小写
我有一个来自Hashtag实体的ArrayCollection(数据库/表排序规则都设置为utf8_bin_ci),我想根据条件匹配它,但它不区分大小写 我尝试了“contains”、“eq”、“startsWith”、“endsWith”表达式,但它在匹配时返回null,但是它有许多结果没有匹配条件 如果我将strtolower/strtoupper添加到Symfony ArrayCollection上的条件表达式区分大小写,symfony,doctrine,criteria,Symfony,Doctrine,Criteria,我有一个来自Hashtag实体的ArrayCollection(数据库/表排序规则都设置为utf8_bin_ci),我想根据条件匹配它,但它不区分大小写 我尝试了“contains”、“eq”、“startsWith”、“endsWith”表达式,但它在匹配时返回null,但是它有许多结果没有匹配条件 如果我将strtolower/strtoupper添加到$this->name属性,它将在指定的情况下工作 如何使其不区分大小写?这里的条件或匹配 您的代码调用将阻止任何数据库魔法的发生,因为它初
$this->name
属性,它将在指定的情况下工作
如何使其不区分大小写?这里的条件或匹配
您的代码调用将阻止任何数据库魔法的发生,因为它初始化集合(加载所有条目),所以您的排序规则并不重要
此外,该函数使用strpos
,这显然不区分大小写
因为您的代码已经拉入了所有的hashtag,所以只需对其进行过滤即可:
$hashtags = array_filter(function(Hashtag $hashtag) {
return stripos($this->name, $hashtag->getName());
}, $this->hashtags);
if(!empty($max)) {
$hashtags = array_slice($hashtags, 0, $max, true);
// remove true, if you don't care about keys ^, this preserves them
}
return $hashtags;
关于@Jakumi的正确答案:
$hashtags = array_filter($this->hashtags->toArray(), function (TwitterHashtag $hashtag) use ($fields) {
foreach ($fields as $field) {
if (false !== stripos($hashtag->getName(), $this->$field)) {
return true;
}
}
});
if (!empty($max)) {
$hashtags = array_slice($hashtags, 0, $max, false);
}
谢谢你,基本上你是对的,但是代码需要一个lil补丁,我认为这个字段是补丁;o)
$hashtags = array_filter($this->hashtags->toArray(), function (TwitterHashtag $hashtag) use ($fields) {
foreach ($fields as $field) {
if (false !== stripos($hashtag->getName(), $this->$field)) {
return true;
}
}
});
if (!empty($max)) {
$hashtags = array_slice($hashtags, 0, $max, false);
}