承認されていない/ドラフトエンティティをフィルタリングする手段として機能するDoctrine2リスナーとフィルターがあります。これは、適用されているエンティティで正常に機能しますが、関係に対して機能させる方法がわかりません。
エンティティがカテゴリと呼ばれているとしましょう。次に、そのカテゴリに関連するfindBy()
製品があり、製品に対して実行するときに、関連するカテゴリが承認されていることを確認するクエリが必要です。
select * from products p
left join category c on p.category_id = c.id
where p.id = 5
and c.approved = true
太字のビットは、フィルターまたは同等のものによって注入する必要があるものです。
これを実装するにはどうすればよいですか?
これまでのところ、フィルターの where の一部としてサブクエリが挿入されていますが、これは地獄のようで、もっと良い方法があるに違いないと考えています。
class ApprovableFilter extends SQLFilter
{
protected $listener;
protected $entityManager;
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
{
$config = $this->getListener()->getConfiguration($this->getEntityManager(), $targetEntity->name);
/* this bit works fine for the category */
if (isset($config['approvable']) && $config['approvable']) {
$column = $targetEntity->columnNames[$config['fieldName']];
return $targetTableAlias.'.'.$column.' = true';
}
/* this bit works for products.. but seems like a pretty poor solution */
if (isset($targetEntity->associationMappings['category'])) {
$config = $this->getListener()->getConfiguration(
$this->getEntityManager(),
$targetEntity->associationMappings['category']['targetEntity']
);
return '(
select d.id from dealership d
where d.id = '.$targetTableAlias.'.dealership_id
and d.'.$config['fieldName'].' = true
) is not null';
}
}