我正在尝试执行一个简单的查询,但我不明白我的错误在哪里。请帮忙!
这是在存储库类中:
public function searchFriends ($param)
{
return $this->getEntityManager()
->createQuery('SELECT f FROM EMMyFriendsBundle:Friend f WHERE f.name='.$param)
->getResult();
}
这就是我从控制器的操作中调用它的方式:
$em = $this->getDoctrine()->getEntityManager();
$result = $em->getRepository('EMMyFriendsBundle:Friend')
->searchFriends($search->getWords());
当它以这种方式时,我收到以下错误:
[Semantical Error] line 0, col 54 near 'Maria': Error: 'Maria' is not defined.
500 Internal Server Error - QueryException
我试图以这种方式将搜索到的值放在引号中:
public function searchFriends ($param)
{
return $this->getEntityManager()
->createQuery('SELECT f FROM EMMyFriendsBundle:Friend f WHERE f.name=" '.$param ' " ')
->getResult();
}
但后来我得到
[Syntax Error] line 0, col 59: Error: Expected end of string, got ' " '
500 Internal Server Error - QueryException
您能告诉我如何正确编写查询吗?提前致谢!
编辑
我也试过这个:
public function searchFriends ($param)
{
$q = $this
->createQueryBuilder('f')
->where('f.name = :name')
->setParameter('name', $param)
->getQuery();
return $q->getResult();
}
有了这个:
$em = $this->getDoctrine()->getEntityManager();
$result = $em->getRepository('EMMyFriendsBundle:Friend')
->searchFriends($search->getWords());
它正在工作