簡単なクエリを実行しようとしていますが、どこが間違っているのかわかりません。助けてください!
これはリポジトリクラスにあります:
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());
そしてそれは働いています