Doctrine 2 Restricting Associations with DQL

后端 未结 1 1725
星月不相逢
星月不相逢 2021-01-16 13:51

There seems to be an over sight in Doctrine 2.1 where it isn\'t easy to return a subset collection for an association.

http://www.doctrine-project.org/docs/orm/2.1/

相关标签:
1条回答
  • 2021-01-16 14:08
    1. Having entitymanager in an entity isn't a good thing in any case (inject your repository instead)
    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.

    0 讨论(0)
提交回复
热议问题