如何在Symfony2中使用LIKE数据库查询

时间:2016-12-02 23:36:09

标签: php symfony orm doctrine-orm repository

我需要知道问题是什么,它应该有效,但它没有取回结果。

有我的控制器:

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

我在存储库中定义了搜索功能,如此

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

}

使用简单的表格

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

我认为它没有收集输入但我无法找到问题

1 个答案:

答案 0 :(得分:0)

更改您的功能存储库:

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