0
我試圖執行一個簡單的查詢,但我不明白我的錯誤在哪裏。請幫忙!錯誤的查詢 - Doctrine2/Symfony2
這是倉庫類:
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());
,它的workiiing
當點是存在的,錯誤是[語法錯誤] 0行,列59:錯誤:預期年底'':(我會嘗試另一種方式, – Faery
你現在的代碼如何?你犯了類似的錯誤...... – adosaiguas
當我複製代碼時,你給出的錯誤仍然是[Syntax Error]行0,col 54:Error:Expected Literal,got''' – Faery