简体   繁体   English

我可以在Doctrine查询中指定默认条件吗?

[英]Can I specify default conditions in Doctrine queries?

I have a basic Doctrine page entity defined. 我定义了基本的Doctrine页面实体。

namespace Example\Entity;

/**
 * @ORM\Entity
 */
class Page
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $title;

    /**
     * @ORM\Column(type="string", length=25)
     */
    private $status;
}

The $status attribute will be one of 'published', 'draft', 'deleted', 'pending'. $ status属性将是“已发布”,“草稿”,“已删除”,“待处理”之一。

I'm wondering if it's possible for all queries on this entity to automatically include the condition WHERE status = 'published' unless the query specifies otherwise. 我想知道是否有可能对该实体上的所有查询自动包含条件WHERE status = 'published'除非查询另有指定。

90% of the queries on this entity will be for published posts, and it will be far too easy for developers to inadvertently grab all draft, pending and deleted posts without realising they should be limiting results. 该实体上90%的查询将针对已发布的帖子,对于开发人员而言,无意中抓住所有草稿,待处理和已删除的帖子而又没有意识到它们会限制结果,这将太容易了。

As far as I can tell, Doctrine's event listeners only apply to data manipulation and not selection. 据我所知,Doctrine的事件侦听器仅适用于数据操作而不适用于选择。

I think that is not possible, the easier solution could be add a custom repository for the entity 我认为这是不可能的,更简单的解决方案是为实体添加自定义存储库

namespace Example\Entity;

/**
 * @ORM\Entity(repositoryClass="Example\Entity\PageRepository")
 */
class Page

Then you can create new methods, for example: 然后,您可以创建新方法,例如:

class PageRepository extends EntityRepository
{
    public function getAllPage()
        {
            return $this->_em->createQuery('SELECT p FROM Example\Entity\Page p WHERE u.status = "published"')->getResult();
        }
}

Or you can overriding the defaults methods of the repository and add you where condition. 或者,您可以覆盖存储库的默认方法,并将您添加到where条件。

documentation: http://docs.doctrine-project.org/en/2.0.x/reference/working-with-objects.html#custom-repositories 文档: http : //docs.doctrine-project.org/en/2.0.x/reference/working-with-objects.html#custom-repositories

I recently have faced such task and solved it like this (but I don't think this is a symfony way:) ): created a class that extends EntityREpository: 我最近遇到了这样的任务,并像这样解决了它(但我不认为这是一种symfony的方式:)):创建了一个扩展EntityREpository的类:

class MainPageRepository extends EntityRepository
{
    protected function createQuery()
    {
        $queryBuilder = $this->getEntityManager()->createQueryBuilder();
        $query = $queryBuilder
            ->select(array('p'))
            ->from('STMainSiteWebBundle:Page', 'p')
            ->where('p.deleted = :deleted')
            ->setParameter('deleted', false);

        return $query;
    }
}

and extended it whenever need to create a query: 并在需要创建查询时对其进行扩展:

class PageRepository extends MainPageRepository
{
    public function fetchAllByType($type, $offset = 0, $limit = 15)
    {
        $query = $this->createQuery();
        $query
            ->andWhere('p.pageType = :pageType')
            ->orderBy('p.id', 'DESC')
            ->setParameter('pageType', $type)
            ->getQuery()
        ;

        $query->setFirstResult($offset);
        $query->setMaxResults($limit);

        return $query->getResult();
    }
}

you can utilize traits: 您可以利用特征:

trait WithStatus
{
    public function createQueryBuilder($alias, $indexBy = NULL)
    {
        $qb = parent::createQueryBuilder($alias, $indexBy);
        $qb->where("$alias.status = :status");
        $qb->setParameter('status', 'published');
        return $qb;
    }
}

class PageRepository extends EntityRepository
{
    use WithStatus;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM