2016-09-06 5 views
1

を検索し、私は(airportairport1departuredateprice)データベースから以下のデータを取得しようとしている、私は[airport]を取得することはできませんが、他のエンティティ。複数の基準は、私が検索フォームを作成したクエリビルダ[Symfony2の]

これは私のコントローラです:あなたのコントローラで

:PostRepository.php

public function searchflight($entity) 
    { 
     $qb = $this->createQueryBuilder('u') 
      ->select('u') 
      ->where('u.airport like :entity') 
      ->andWhere('u.airport1 like :entity') 
      ->orderBy('u.id') 
      ->setParameter('entity', '%'.$entity.'%'); 
     return $qb->getQuery()->getResult(); 
    } 

答えて

1

のようなものを試してみてください

public function searchtabflightResultAction(Request $request) 
{ 


    $form = $this->createForm(new SearchflightType()); 

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

    $em = $this->getDoctrine()->getManager(); 
    $entities = $em->getRepository('FLYBookingsBundle:Post')->searchflight($form['airport']->getData()); 

    } else { 
     throw $this->createNotFoundException('The page you were looking for doesn\'t exist.'); 
    } 

    return $this->render('FLYBookingsBundle:Post:searchtabflightResult.html.twig', array('entities' => $entities)); 
} 

あなたのレポで
$entities = $em->getRepository('FLYBookingsBundle:Post')->searchflight($form); 

public function searchflight(FormInterface $form) 
{ 
    $qb = $this->createQueryBuilder('u'); 

    if ($form->has('airport')) { 
     $qb->andWhere(
      $qb->expr()->like('u.airport', ':airport') 
     ) 
      ->setParameter('airport', '%'. $form->get('airport')->getData().'%') 
    } 

    // ... complete like this with the other params 
+0

はご回答いただきありがとうございます。私はあなたのクエリを試して、それは価格の実体でうまくいくが、私はそれが何か結果を見つけることができない空港を探しているとき、私はあまりにも厳しいためだと思う。私のクエリビルダーでは、この ''% '。$ entity。'% ''が好きでした。どうすればそれを厳しくしないのですか? – Sirius

関連する問題