ORM/条令为ArrayCollections设置了多个过滤器


ORM / Doctrine set multiple filters for ArrayCollections

我以这种方式过滤ArrayCollection:

$result = $eCollection->getElements()->filter(
   function($rObject) use ($id) {
      return in_array($rObject->getEid(), array($id));
   }
);

但是这个集合还有一个名为type的属性和一个称为date的属性。

如何根据ID和TYPE或到值之间的DATE对其进行筛选?

我认为您应该查看Doctrine'Common'Collections'Criteria。您可以阅读8.8章中关于标准的所有内容。原则2文档中的筛选集合。使用条件的优点是可以添加各种约束,并且将在数据库级别执行。这意味着您不会不必要地在集合中加载任何不想要的元素。

$elements = $eCollection->getElements();
$criteria = Criteria::create()
    ->where(Criteria::expr()->eq("id", $id))
    ->andWhere(Criteria::expr()->eq("type", $type))
    ->andWhere(Criteria::expr()->between('date', $from, $to));
$result = $elements->matching($criteria);