我在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(),
);
}
}
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时,我没有得到任何结果。 所以视图是空的!有人可以看看代码并告诉我有什么问题吗?谢谢你的时间
答案 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(),
);
}
}