2016-09-09 10 views
1

Doctrineの問合せビルダーを使用して動的問合せを作成しようとしています。私はオプションのパラメータを持っています。どのように私は、ステートメントに条件付きでこれを追加しますか?私はまた、団体(例えば、ServiceCategoryへの多対1の関係にある)Doctrine DQL条件付き問合せ(結合あり)

を持って
$qb = $this->createQueryBuilder('service'); 
$qb->join('service.category', 'category'); 

$conditions = array(); 

if ($categoryId != null) { 

     $category = $this->getEntityManager() 
      ->getRepository('AppBundle:Category')->find($categoryId); 

     if($category == null){ 
      throw new ApiException('category not found'); 
     } 

     $conditions[] = $qb->expr()->like('category.path', $category->getPath().'%'); 
    } 

if ($userId != null) { 
     $user = $this->getEntityManager()->getRepository('AppBundle:User')->find($userId); 
     if($user == null){ 
      throw new ApiException('user not found'); 
     } 

     $conditions[] = $qb->expr()->eq('service.user', $userId); 
    } 

    if ($rating != null) { 
     $conditions[] = $qb->expr()->gte('service.rating', $rating); 
    } 

$conditions = call_user_func_array(array($qb->expr(), 'andX'), $conditions); 

$qb->where($conditions); 

$qb->addOrderBy('service.created', 'DESC'); 

return $qb; 
} 

私は、クエリ

http://127.0.0.1:8000/api/services?limit=10&categoryId=45 

を送信しようとすると私が取得次のエラー:

Error: Expected StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression, got "45" (500 Internal Server Error) 
+0

エラーは '$区分= 45'が、明確ではないが、どのような値に関連するように見える

$conditions[] = $qb->expr()->gte('service.rating', $rating); 

希望を'$ category-> getPath()'のどれか? – yceruto

答えて

0

与えられた引数のリテラル式を試してみてください。だから、これを試してみてください。これに代えて

$conditions[] = $qb->expr()->gte('service.rating', $this->expr()->literal($rating)); 

:このヘルプ

+0

'$ rating'は数字ですか?なぜこの 'service.rating> '1''をする必要がありますか? – yceruto