Doctrine和Like查询symfony2

时间:2014-06-18 17:55:44

标签: symfony doctrine dql

我的页面中有一个搜索栏,我负责查找用户搜索内容的操作是:

 public function searchAction(Request $request){

        $em = $this->container->get('doctrine')->getEntityManager();

        $evenements= $em->getRepository('Mql14mqlmeBundle:Evenement')->findAll();
        if ('POST' === $request->getMethod()) {

            $search = $request->get('search');
    $query = $this->container->get('doctrine')->getEntityManager()->createQuery( 'SELECT e FROM Mql14mqlmeBundle:Evenement e WHERE e.nom LIKE :search') 
             ->setParameter('search', $search);
        $resultats = $query->getResult();



        return $this->container->get('templating')->renderResponse('Mql14mqlmeBundle:Event:search.html.twig', array(
            'resultats'=>$resultats,

        ));

        }
   return $this->listerAction();
        }

如果用户在数据库中输入了某个事件的确切名称,它就可以正常工作,但我想让搜索成为可能,即使它只是名称的一部分,我在查询:

    $query = $this->container->get('doctrine')->getEntityManager()->createQuery( 'SELECT e FROM Mql14mqlmeBundle:Evenement e WHERE e.nom LIKE :%search%') 
               ->setParameter('search', $search);

但我收到此错误:参数格式无效,:给定,但是:name或?num expected。

1 个答案:

答案 0 :(得分:1)

尝试更改此参数:

$query = $this
    ->container
    ->get('doctrine')
    ->getEntityManager()
    ->createQuery(
        'SELECT e FROM Mql14mqlmeBundle:Evenement e WHERE e.nom LIKE :search'
    ) 
    ->setParameter('search', '%'.$search.'%');