6

我有一个 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';
        }
    }
4

2 回答 2

2

使用注释可以使您的解决方案更加通用。我写了一个关于它的要点:https ://gist.github.com/technetium/0c62164400a411e9ffc3713260448b25

于 2016-04-14T07:51:34.653 回答
1

我能想到的最好的方法是通过 Category 对象检索您的 Products。
这样你只需要过滤 category.approved 字段。

例如:

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';
    }
}

然后你的 Category 实体应该有一个 products 集合(假设你有一个双向关系)。

use Doctrine\Common\Collections\ArrayCollection;

class Category {
    /**
     * @var ArrayCollection $products
     * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
     */
    private $products;

    public function __construct()
    {
        $this->products = new ArrayCollection;
    }

    public function getProducts()
    {
        return $this->products;
    }
}

这样您就可以首先检索您的类别

$category = $this->get('doctrine')->getRepository('SomeBundle:Category')->find(5);
if( $category ) {
    //Here you now the category is approved
    $products = $category->getProducts();
}

希望这可以帮助。

编辑:

回答@lracicot 问题并举例说明单向关系:我将创建一个 ProjectRepository 方法,例如:

...
findByCategoryApproved( $product_id, $approved = true )
{
    $query =
        'SELECT p
         FROM AcmeBundle:Product
         LEFT JOIN p.categories c
         WHERE p.id = :id AND c.approved = :approved';
    
    return $this
        ->getEntityManager()
        ->createQuery( $query )
        ->setParameter( 'id', $product_id )
        ->setParameter( 'approved', $approved )
        ->getOneOrNullResult();
}

...

$product = $doctrine
     ->getRepository('AcmeBundle:Product')
     ->findByCategoryApproved(5);
于 2013-01-14T20:28:07.040 回答