2016-12-02 58 views
-1

請我需要知道什麼是問題,它應該工作,但它沒有得到結果。如何在Symfony2中使用一個LIKE數據庫查詢

有我的控制器:

public function searchAction() 
{ 
    $form = $this->createForm(new SearchCoursesType()); 
    $request = $this->getRequest(); 
    if($request->getMethod() == 'POST') 
    { 
     $form->bind($request); 
     if($form->isValid()) 
     { 
      $em = $this->getDoctrine()->getManager(); 
      $data = $this->getRequest()->request->get('formation_appbundle_scourse'); 
      $courses = $em->getRepository('FormationAppBundle:Course')->findCoursesByParametres($data); 
      return $this->render('FormationAppBundle:Courses:coursesList.html.twig', array('courses' => $courses)); 
     } 
    } 
    return $this->render('FormationAppBundle:Template:searchIndex.html.twig', array('form' => $form->createView())); 
} 

我用一個簡單的表格

{{ form_start(form) }} 
    {{ form_widget(form) }} 
<input type="submit" value="Create" /> 
{{ form_end(form) }} 

我覺得這沒」在庫中定義的搜索功能,這樣

public function findCoursesByParametres($data) 

{ 

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

    $query->where($query->expr()->like('a.title', ':title')) 

     ->setParameters(array(

      'title' => $data['title'] 
     )); 
    return $query->getQuery()->getResult(); 

} 

t收集輸入,但我找不到問題

+0

$數據= $形式 - >的getData();應該得到你的頭銜。但沒有一些%字符的情況下使用LIKE沒有什麼意義。 – Cerad

回答

0

改變你的函數庫:

public function findCoursesByParametres($data) 
{ 
    $query = $this->createQueryBuilder('a') 
       ->where('a.title LIKE :title') 
       ->setParameters(array(
        'title' => '%' . $data['title'] . '%' 
      )); 
    return $query->getQuery()->getResult(); 
    } 
相關問題