2017-03-29 8 views
0

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 
+0

getQuery() 실행하지 마십시오 -/전> getResult를을()에서 각각. – Maerlyn

+0

시도해 봤는데, 작동하지 않았다 – bigmammoo

답변

1

귀하의 $shrubs 변수는 당신이 부르는 $query 결과 ->getQuery()->getResult()

귀하의 코드는

과 같을 것이다 고정

$repository = $this->getDoctrine()->getRepository('AppBundle:Shrubs'); 

$query = $repository->createQueryBuilder('p'); 

$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') 
    ; // Remove ->getQuery() and->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 = $query->andWhere('$key = x') 
      ->setParameter('x', $key) 
      ->getQuery() 
      ->getResult(); 
     return $this->render('shrubs/searchresults.html.twig', array(
      'shrubs' => $shrubs,)); 
    } 
} 

$shrubs = $query->getQuery()->getResult();// Execute the query here 

return $this->render('shrubs/searchresults.html.twig', array(
    'shrubs' => $shrubs 
); 
+0

그것은 말합니다 : "[구문 오류] 라인 0, 열 120 : 오류 : 예상 리터럴, '$'"있어. 우리가 점점 더 가까워지고있는 것 같아 ... – bigmammoo

+1

오류는'$ query-> andWhere ('$ key = x')'행에 대한 것이고, 여기서 무엇을 이루려고하는지 모르겠지만'$ key '는 열 이름으로되어 있기 때문에,'$ key-> andWhere ('p. 'key : = : x')'(당신은':'도 잊어 버렸습니다.) – DFayet

+2

And ($ {$ key} == "x")도 도움이되지 않습니다. foreach 루프 전체가 의미가 없습니다. 단지 관목을 걸러 내고 싶다면 array_filter를 사용하십시오. – Cerad