Forms 如何筛选Symfony2表单集合中显示的实体

Forms 如何筛选Symfony2表单集合中显示的实体,forms,symfony,Forms,Symfony,是否有方法筛选Symfony2在表单集合中收集的实体 我的设想是这样的 2个实体:父实体和子实体。 子实体具有属性“birthdate”。 这两个表之间有许多关系 我有一个formType(parentType),其中包含一组childType表单 我可以加载parentType,它会加载与父记录关联的每个子类型 我想过滤childType集合,以便包含生日大于日期的记录,排除生日小于日期的记录 Symfony2集合表单类型不允许使用“查询生成器”在生成器->添加()中过滤选择 有人遇到或解决过

是否有方法筛选Symfony2在表单集合中收集的实体

我的设想是这样的

2个实体:父实体和子实体。 子实体具有属性“birthdate”。 这两个表之间有许多关系

我有一个formType(parentType),其中包含一组childType表单

我可以加载parentType,它会加载与父记录关联的每个子类型

我想过滤childType集合,以便包含生日大于日期的记录,排除生日小于日期的记录

Symfony2集合表单类型不允许使用“查询生成器”在生成器->添加()中过滤选择


有人遇到或解决过这个问题吗?

我的解决方案是对子实体集合使用单独的setter/getter,并使用条件筛选getter输出。表单字段名称应为“filteredChilds”。有点不对劲,但应该能奏效

Entity/Parent.php

<?php

...

use Doctrine\Common\Collections\Criteria;

...

class Parent
{

    ...

    /**
     * @param Child $child
     * @return $this
     */
    public function addChild(Child $child)
    {
        $this->childs[] = $child;

        return $this;
    }

    /**
     * @param Child $child
     */
    public function removeChild(Child $child)
    {
        $this->childs->removeElement($child);
    }

    /**
     * @return ArrayCollection
     */
    public function getChilds()
    {
        return $this->childs;
    }

    /**
     * @param Child $child
     * @return $this
     */
    public function addFilteredChild(Child $child)
    {
        $this->addChild($child);
    }

    /**
     * @param Child $child
     */
    public function removeFilteredChild(Child $child)
    {
        $this->removeChild($child);
    }

    /**
     * @return ArrayCollection
     */
    public function getFilteredChilds()
    {
        $criteria = Criteria::create()
            ->where(Criteria::expr()->gt("birthday", new \DateTime()));

        return $this->getChilds()->matching($criteria);
    }

    ...

}