如何在一个可以具有不同值的属性上查找

How to findBy on one attribute which can be of different values

我正在尝试显示一个 table,其中用户将通过在表单中​​选择不同的选项来决定将显示哪些对象。

 if ($form->isValid()) {
        $type = $form->get('type')->getData();
        //Getting values from Array $type returned by getData on the form
        foreach ($type as $livraison) {

            $elements = $this->getDoctrine()
                    ->getManager()
                    ->getRepository('etaqenregistrementBundle:Livraison')
                    ->findBy(array('type' => $livraison));
        }

    }

如果我们选择了两个选项,$type 将是一个包含两个不同对象 $livraison 的数组(var_dump($livraison) 在 foreach 语句中):

object(etaq\adminBundle\Entity\typeLivraison)[434] private 'id' => int 1 private 'valeur' => string 'Interne' (length=7)

object(etaq\adminBundle\Entity\typeLivraison)[436] private 'id' => int 3 private 'valeur' => string 'Autorité' (length=9)

但是 findBy 方法 returns 只有一个对象,即在表单中第一个被选中的对象。

如果用户在表单中选择了一个参数,我想要的是对一个参数进行排序,或者如果他选择了两个或三个,则对两个参数进行排序......等等

这是我的表格:

    $form = $this->createFormBuilder()
            ->add('ecart', 'choice', array(
                'label' => 'Only late :',
                'choices' => array(true => 'Yes', false => 'No'),
                'multiple' => false,
                'expanded' => true,
                'required' => true,
                'empty_data' => false))
            ->add('type', 'entity', array(
                'label' => ' Type :',
                'class' => 'etaqadminBundle:typeLivraison',
                'empty_data' => false,
                'required' => true,
                'property' => 'valeur',
                'multiple' => true,
                'expanded' => true))
            ->getForm();

谢谢大家。

您可以使用查询 (http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html) or query builder (http://docs.doctrine-project.org/en/latest/reference/query-builder.html)

$qb = $this->getDoctrine()
                ->getManager()
                ->createQueryBuilder();
$elements = $qb->from('etaqenregistrementBundle:Livraison', 'l')
                ->select('l')
                ->where(qb->expr()->in('l.type', ':ids'))
                ->setParameter('ids', $livraisonIds)
                ->getQuery()
                ->getResult();

findBy 可以 return 多个值:

If you pass an array of values Doctrine will convert the query into a WHERE field IN (..) query automatically: Doctrine 2 Docs.

获取所有id,然后将它们全部发送到findBy。

if ($form->isValid()) {
        $type = $form->get('type')->getData();
        $livRaisonsIds = array();
        //Getting values from Array $type returned by getData on the form
        foreach ($type as $livraison) {
             // Something similar, there might be a setter to get the id since its private.
             $livRaisonsIds[] = $livraison->id;                 
        }

        $elements = $this->getDoctrine()
                ->getManager()
                ->getRepository('etaqenregistrementBundle:Livraison')
                ->findBy(array('type' => $livRaisonsIds));
}