symfony doctrine查询不起作用

时间:2013-12-19 00:55:24

标签: php symfony doctrine-orm

我在2个实体Guest和Party之间有一个ManyToMany关系:

//Prif\ProtocoleBundle\Entity\Guest

/**
 * @var string
 * 
 * @ORM\ManyToMany(targetEntity="Prif\ProtocoleBundle\Entity\Party", cascade={"persist"})
 * 
 */
private $parties;

我制作了一个表单,我可以使用复选框为访客添加多方

  \\Prif\ProtocoleBundle\Form\GuestType

  public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder

            ->add('name', 'text', array(
                'required'=>true))

            ->add('firstname', 'text', array(
                'required'=>true)) 

            ->add('parties', 'entity', array(
                'class' => 'PrifProtocoleBundle:Party',
                'property' => 'name',
                'expanded' => true,
                'multiple' => true,
                'required'=>false
            ))

然后我做了一个搜索表单(PartySearchType),我可以选择一个或多个聚会。当我提交此表格时,我想邀请已加入这些派对的客人

这是我在存储库中的搜索功能

//Prif\ProtocoleBundle\Entity\GuestRepository
/**
 * Summary
 * @param   object  $name_parties           Description
 * 
 * @return  object                  Description
 */
public function searchGuestByParty(array $name_parties) {

    $query = $this->createQueryBuilder('g');

        $query->Join('g.parties', 'p')
              ->where($query->expr()->in('p.name', $name_parties));


        return $query->getQuery()
                ->getResult();

我的搜索控制器中的操作

/**
 * @Route("/", name="guest)
 * @Template()
 */
public function searchAction(Request $request) {
    $entity = new Guest();

    $form = $this->createForm(new PartySearchType(), $entity);

    if ($request->getMethod() == 'POST') {
        $form->handleRequest($request);

        $parties = $form['parties']->getData();
        //var_dump($parties) this var_dump shows me a result of selected parties
            $repository = $this->getDoctrine()
                    ->getManager()
                    ->getRepository('PrifProtocoleBundle:Guest');

            $guests = $repository->searchGuestByParty(array($parties));
            //var_dump($guests) this var_dump shows me: array (size=0) empty;

            return $this->render('PrifProtocoleBundle:Guest:result.html.twig', array(
                        'entities' => $guests,
                            )
            );
    }
    return array(
        'form' => $form->createView(),
    );
}

}

var_dump($ guests)的结果:

array (size=0) empty

树枝视图的一部分

\\result.html.twig

{% block body -%}
<h1>List of guests </h1>
<table class="records_list">
    <thead>
        <tr>
            <th>name</th>
            <th>surname</th>
        </tr>
    </thead>
    <tbody>
    {% for entity in entities %}
        <tr >
            <td>{{ entity.name }}</td>
            <td>{{ entity.surname }}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>
{% endblock %}

我的问题:

我不确定我在GuestRepository函数中的查询,以及我在控制器中调用它的方式,因为当我对它进行var_dump时,我没有得到任何结果。 所以视图是空的!有人可以看看代码并告诉我有什么问题吗?谢谢你的时间

2 个答案:

答案 0 :(得分:0)

乍一看,您似乎将$ name_parties传递给searchGuestByParty,但在where子句中使用的是$ nom_name_parties,一个未初始化的变量。可能就这么简单吗?

答案 1 :(得分:0)

我已经改变了存储库功能,现在它可以工作了!

根据表单中选择的各方

检索来宾
public function searchGuestByParty($parties) {

    $name_parties = array();


    for($i = 0; $i < count($parties); ++$i){

        $name_parties[$i]= $parties[$i]->getName();
    }

    $query = $this->createQueryBuilder('g');

    $query->Join('g.parties', 'p')
          ->where($query->expr()->in('p.Name', $name_parties));


    return $query->getQuery()
                    ->getResult();
}

控制器中的搜索操作不会发生太大变化

public function searchAction(Request $request) {
$entity = new Guest();

$form = $this->createForm(new PartySearchType(), $entity);

if ($request->getMethod() == 'POST') {
    $form->handleRequest($request);

    $parties = $form['parties']->getData();

        $repository = $this->getDoctrine()
                ->getManager()
                ->getRepository('PrifProtocoleBundle:Guest');

        $guests = $repository->searchGuestByParty($parties);


        return $this->render('PrifProtocoleBundle:Guest:result.html.twig', array(
                    'entities' => $guests,
                        )
        );
}
return array(
    'form' => $form->createView(),
);

}

}