简体   繁体   English

DQL准则2限制协会

[英]Doctrine 2 Restricting Associations with DQL

There seems to be an over sight in Doctrine 2.1 where it isn't easy to return a subset collection for an association. 在Doctrine 2.1中似乎有些疏漏,在其中返回关联的子集并不容易。

http://www.doctrine-project.org/docs/orm/2.1/en/reference/limitations-and-known-issues.html#restricing-associations http://www.doctrine-project.org/docs/orm/2.1/en/reference/limitations-and-known-issues.html#restricing-associations

The docs recommend to write a repository find method, which makes sense because that was the first thing I though of doing. 文档建议编写一个存储库查找方法,这很有意义,因为这是我要做的第一件事。

However without having a reference to the EntityManager within an Entity I can't see how you would retrieve the association's Repository and this seems to defeat the point of separating the Domain from the Database? 但是,如果没有在实体中引用EntityManager,我看不到如何检索关联的存储库,这似乎使将域与数据库分离的观点变得不对头了?

Is there a recommended strategy for this problem? 有针对此问题的推荐策略吗?

Here is my interpretation of their suggested solution. 这是我对他们建议的解决方案的解释。

class Category
{
    protected $id;
    protected $articles; // PesistentCollection
    protected $em; // The EntityManager from somewhere?

    public function getVisableArticles()
    {
        return $this->em->getRepository('Article')
                    ->getVisibleByCategory($this);
    }
}
  1. Having entitymanager in an entity isn't a good thing in any case (inject your repository instead) 在任何情况下,在实体中拥有entitymanager都不是一件好事(改为注入您的存储库)
  2. Category isn't the only root for articles because it can't daterimne what articles you need, so you need a repository for articles. 类别不是文章的唯一根源,因为它无法确定所需的文章,因此您需要一个文章存储库。

What i would do: 我会怎么做:

class Category
{
    protected $id;
    protected $articles; // PesistentCollection

    public function getVisableArticles(IArticleRepository $articleRepository)
    {
        return $articleRepository->getVisibleByCategory($this);
    }
}

interface IArticleRepository
{
    function getVisibleByCategory(Category $category);
}

Your doctrine's repository would implement IArticleRepository and the class won't know anything about your data storage/doctrine. 您的学说的存储库将实现IArticleRepository,并且该类对您的数据存储/学说一无所知。

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

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