ID で「製品」を検索しようとしていますが、ロケールとアクティブな状態の 2 つの条件ですべての「写真」に参加しようとしています。
ここに私の QueryBuilder があります:
$queryBuilder = $this->createQueryBuilder('p') ->select('p, photos, photoTranslation') ->leftJoin('p.photos', 'photos') ->leftJoin('photos.translations', 'photoTranslation') ->where('p.id = :id') ->andWhere('(photoTranslation.locale = :locale OR photoTranslation.locale IS NULL)') ->andWhere('(photoTranslation.active = :active OR photoTranslation.active IS NULL)') ->setParameters(array( 'id' => $id 'locale' => $this->getLocale(), 'active' => true ));
写真がない場合、またはアクティブな写真がある場合は正常に機能しますが、非アクティブな写真がある場合は、2 つの条件のいずれにも一致しないため機能しません。
ロケール部分のみなど、1 つの条件のみを使用すると、正常に動作します。
$queryBuilder = $this->createQueryBuilder('p') ->select('p, photos, photoTranslation') ->leftJoin('p.photos', 'photos') ->leftJoin('photos.translations', 'photoTranslation') ->where('p.id = :id') ->andWhere('(photoTranslation.locale = :locale OR photoTranslation.locale IS NULL)') ->setParameters(array( 'id' => $id 'locale' => $this->getLocale() ));
今のところ、これらの結果をループして、非アクティブな写真をすべて設定解除します...しかし、QueryBuilder で行うためのクリーンな方法が必要です。
また、LEFT JOIN 句に条件を付けようとしました。
->leftJoin('photo.translations', 'phototTranslation', Doctrine\ORM\Query\Expr\JOIN::WITH, 'photoTranslation.locale = :locale AND photoTranslation.active = :active')
ただし、アクティブでない場合でも、常に Photo を返します。