1
Doctrineの問合せビルダーを使用して動的問合せを作成しようとしています。私はオプションのパラメータを持っています。どのように私は、ステートメントに条件付きでこれを追加しますか?私はまた、団体(例えば、Service
がCategory
への多対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)
エラーは '$区分= 45'が、明確ではないが、どのような値に関連するように見える
希望を'$ category-> getPath()'のどれか? – yceruto