与DQL的学说2限制关联 [英] Doctrine 2 Restricting Associations with DQL

查看:69
本文介绍了与DQL的学说2限制关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Doctrine 2.1中似乎有一个不为人知的地方,要返回一个关联的子集
并不容易。



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



文档建议编写一个存储库查找方法,这很有意义,因为那是我要做的第一件事。



但是在实体中不引用EntityManager时,我可以



有没有建议的解决此问题的策略? / p>

这是我对他们建议的解决方案的解释。

  class Category 
{
受保护的$ id;
受保护的物品; // PesistentCollection
受保护的$ em; // EntityManager是从哪里来的?

公共函数getVisableArticles()
{
return $ this-> em-> getRepository('Article')
-> getVisibleByCategory($ this) ;
}
}


解决方案


  1. 在任何情况下,在实体中拥有EntityManager都不是一件好事。
    (改为注入您的存储库)

  2. 类别不是文章的唯一根源因为它无法确定所需的文章,所以您需要一个文章存储库。

我会做什么:

 类别类别
{
受保护的$ id;
受保护的物品; // PesistentCollection

公共函数getVisableArticles(IArticleRepository $ articleRepository)
{
return $ articleRepository-> getVisibleByCategory($ this);
}
}

接口IArticleRepository
{
函数getVisibleByCategory(Category $ category);
}

您的学说的存储库将实现IArticleRepository,并且该类对您一无所知数据存储/原则。


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/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?

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)
  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.

这篇关于与DQL的学说2限制关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆