我有一个Doctrine2监听器和过滤器,作为过滤掉任何未批准/草稿实体的手段,在应用于它的实体上运行良好,但是,我不知道如何使它在其关系中起作用。例如,如果该实体称为“类别”,则我将与该类别相关的产品,当我对产品进行
findBy()
时,需要查询以确保它们所属的类别已经被批准。加粗的部分是我的过滤器或等效物需要注入的内容。我该如何实现这一点?到目前为止,我在过滤器中注入了子查询作为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';
}
}