简体   繁体   English

symfony主义查询不起作用

[英]symfony doctrine query is not working

i have a ManyToMany relation between 2 entities Guest and Party : 我有2个实体Guest和Party之间的ManyToMany关系:

//Prif\ProtocoleBundle\Entity\Guest

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

i made a form where i can add many parties to a Guest with checkboxes 我做了一个表格,可以在其中添加许多方到复选框

  \\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
            ))

Then i made a search form (PartySearchType) where i can select one or many parties. 然后,我制作了一个搜索表单(PartySearchType),可以在其中选择一个或多个参与方。 and when i submit this form i want to have the guests that have been added to those parties 当我提交此表格时,我想邀请已添加到这些聚会中的来宾

This is my search function in the repository 这是我在存储库中的搜索功能

//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();

My search Action in the controller 我在控制器中的搜索操作

/**
 * @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(),
    );
}

}

result of var_dump($guests): var_dump($ guests)的结果:

array (size=0) empty

part of the twig view 树枝视图的一部分

\\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 %}

My problem: 我的问题:

i'm not sure of my query in GuestRepository function, and of the way i call it in the controller, coz when i make a var_dump on it i don't get no results. 我不确定GuestRepository函数中的查询,以及我在控制器中调用它的方式,因为当我在其上执行var_dump时我没有得到任何结果。 so the view is empty! 所以视图是空的! can someone take a look at the code and tell me what's wrong? 有人可以看一下代码,然后告诉我怎么了吗? thanks for your time 谢谢你的时间

At first glance, it appears you are passing $name_parties to searchGuestByParty but in the where clause you are using $nom_name_parties, an uninitialized variable. 乍一看,您似乎将$ name_parties传递给searchGuestByParty,但是在where子句中,您使用的是未初始化的变量$ nom_name_parties。 Could it be that simple? 可以这么简单吗?

i've changed the repository function, now it works! 我已经更改了存储库功能,现在可以使用了!

it retrieves the guests according to the parties selected in the form 它根据表单中选择的各方检索来宾

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();
}

the search action in the controller doesn't change much 控制器中的搜索动作变化不大

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(),
);

} }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM