QueryBuilderクエリを実行しようとしていますが、forループの "andWhere"構文が嫌いです。私はそれがforループで何も気に入らないと思う。配列要素のどれかが "x"に等しいかどうかを調べることで、それに基づいて検索結果をフィルタリングします。Symfony 2エラー:配列のメンバー関数andWhere()を呼び出す
$repository = $this->getDoctrine()->getRepository('AppBundle:Shrubs');
$query = $repository->createQueryBuilder('p');
$shrubs = $query
->where($query->expr()->like('p.botanicalname', ':botanicalname'))
->setParameter('botanicalname', '%' . $botanicalname . '%')
->andwhere($query->expr()->like('p.commonname', ':commonname'))
->setParameter('commonname', '%' . $commonname . '%')
->orderBy('p.commonname', 'ASC')
->getQuery()
->getResult();
$checkfor = array("wetsoil"=>"Tolerates Wet Soil",
"borderlinehardy"=>"Borderline Hardy",
"moistsoil"=>"Prefers Moist Soil";
reset($checkfor);
foreach ($checkfor as $key => $value) {
if (${$key} == "x") {
$shrubs = $shrubs->andWhere('$key = x')
->setParameter('x', $key)
->getQuery()
->getResult();
return $this->render('shrubs/searchresults.html.twig', array(
'shrubs' => $shrubs,));
}
}
return $this->render('shrubs/searchresults.html.twig', array(
'shrubs' => $shrubs
getQuery()を実行しないでください - /前>のgetResultを()で、あなたのforeach。 – Maerlyn
試してみましたが、うまくいきませんでした – bigmammoo