![](/img/trans.png)
[英]Symfony Doctrine query builder LIKE not working in PostgreSQL
[英]symfony doctrine query is not working
我有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時我沒有得到任何結果。 所以視圖是空的! 有人可以看一下代碼,然后告訴我怎么了嗎? 謝謝你的時間
乍一看,您似乎將$ name_parties傳遞給searchGuestByParty,但是在where子句中,您使用的是未初始化的變量$ nom_name_parties。 可以這么簡單嗎?
它根據表單中選擇的各方檢索來賓
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(),
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.