[英]Doctrine 2 Restricting Associations with DQL
在Doctrine 2.1中似乎有些疏漏,在其中返回关联的子集并不容易。
文档建议编写一个存储库查找方法,这很有意义,因为这是我要做的第一件事。
但是,如果没有在实体中引用EntityManager,我看不到如何检索关联的存储库,这似乎使将域与数据库分离的观点变得不对头了?
有针对此问题的推荐策略吗?
这是我对他们建议的解决方案的解释。
class Category
{
protected $id;
protected $articles; // PesistentCollection
protected $em; // The EntityManager from somewhere?
public function getVisableArticles()
{
return $this->em->getRepository('Article')
->getVisibleByCategory($this);
}
}
我会怎么做:
class Category
{
protected $id;
protected $articles; // PesistentCollection
public function getVisableArticles(IArticleRepository $articleRepository)
{
return $articleRepository->getVisibleByCategory($this);
}
}
interface IArticleRepository
{
function getVisibleByCategory(Category $category);
}
您的学说的存储库将实现IArticleRepository,并且该类对您的数据存储/学说一无所知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.