ArrayCollection 上的条件表达式区分大小写
Criteria expression on ArrayCollection is case sensitive
我有一个来自 Hashtag 实体的 ArrayCollection(database/table 归类都设置为 utf8_bin_ci),我想根据条件匹配它,但它 不区分大小写 .
我试过 'contains'、'eq'、'startsWith'、'endsWith' 表达式,但它 returns 匹配时为空,但是它有很多结果没有条件匹配。
如果我将 strtolower / strtoupper 添加到 $this->name
属性,它会在指定的情况下工作。
如何使条件或此处的匹配不区分大小写?
public function getHashtags($max = null) {
$hashtags = $this->hashtags;
$hashtags->getIterator();
$crit = Criteria::create()
->where(Criteria::expr()->contains('name', $this->name))
if (!empty($max)) {
$crit->setMaxResults($max);
}
return $hashtags->matching($crit);
}
您的代码调用 getIterator()
,这将阻止任何数据库魔术的发生,因为它会初始化集合(加载所有条目),所以您的排序规则无论如何都无关紧要。
另外,contains
函数使用了strpos
,显然不区分大小写。
由于您的代码已经提取了所有主题标签,因此只需对其进行过滤即可:
$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);
}
我有一个来自 Hashtag 实体的 ArrayCollection(database/table 归类都设置为 utf8_bin_ci),我想根据条件匹配它,但它 不区分大小写 .
我试过 'contains'、'eq'、'startsWith'、'endsWith' 表达式,但它 returns 匹配时为空,但是它有很多结果没有条件匹配。
如果我将 strtolower / strtoupper 添加到 $this->name
属性,它会在指定的情况下工作。
如何使条件或此处的匹配不区分大小写?
public function getHashtags($max = null) {
$hashtags = $this->hashtags;
$hashtags->getIterator();
$crit = Criteria::create()
->where(Criteria::expr()->contains('name', $this->name))
if (!empty($max)) {
$crit->setMaxResults($max);
}
return $hashtags->matching($crit);
}
您的代码调用 getIterator()
,这将阻止任何数据库魔术的发生,因为它会初始化集合(加载所有条目),所以您的排序规则无论如何都无关紧要。
另外,contains
函数使用了strpos
,显然不区分大小写。
由于您的代码已经提取了所有主题标签,因此只需对其进行过滤即可:
$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);
}