我可以在使用惰性加载的时候过滤掉Doctrine 2中arrayCollection的结果吗?例如,Doctrine 2 ArrayCollection过滤方法
// users = ArrayCollection with User entities containing an "active" property
$customer->users->filter('active' => TRUE)->first()
目前还不清楚过滤方法是如何实际使用的。
我可以在使用惰性加载的时候过滤掉Doctrine 2中arrayCollection的结果吗?例如,Doctrine 2 ArrayCollection过滤方法
// users = ArrayCollection with User entities containing an "active" property
$customer->users->filter('active' => TRUE)->first()
目前还不清楚过滤方法是如何实际使用的。
的鲍里斯·伽利湖答案的这个职位,可以帮助你: Doctrine 2, query inside entities
$idsToFilter = array(1,2,3,4);
$member->getComments()->filter(
function($entry) use ($idsToFilter) {
return in_array($entry->getId(), $idsToFilter);
}
);
您的使用情况是:
$ArrayCollectionOfActiveUsers = $customer->users->filter(function($user) {
return $user->getActive() === TRUE;
});
如果添加 - >第(),你会得到只有返回的第一个条目,这不是你想要的。
@ Sjwdavies 您需要将()传递给USE的变量。您也可以缩短为in_array回报是一个布尔值已经:
$member->getComments()->filter(function($entry) use ($idsToFilter) {
return in_array($entry->getId(), $idsToFilter);
});
原则现在有Criteria
,它提供了一个API,用于根据上下文使用SQL和PHP过滤集合。
更新
这将实现结果的接受的答案,而不从数据库中获取的一切。
use Doctrine\Common\Collections\Criteria;
/**
* @ORM\Entity
*/
class Member {
// ...
public function getCommentsFiltered($ids) {
$criteria = Criteria::create()->where(Criteria::expr()->in("id", $ids));
return $this->getComments()->matching($criteria);
}
}
感谢您清除这将实际上改变sql查询,而不是选择数据库中的所有内容,然后通过循环应用过滤器! – tftd
http:// stackoverflow。com/questions/35358597/in-predicate-with-criteria-filtering-isnt-working please,看看这里:) – DonCallisto
在集合上使用'indexBy =“xxx”'并调用'匹配时出现一个问题'在上面,索引被丢弃。 https://github.com/doctrine/doctrine2/issues/4693 – fyrye
但仍然方法加载到许多数据,例如计数,加载所有匹配的数据。 – 2013-09-19 23:05:08