Doctine 2限制与DQL的关联 [英] Doctine 2 Restricting Associations with DQL
问题描述
文档建议写一个存储库查找方法,这是有道理的,因为这是我第一件事情,尽管如此。
然而,没有引用实体内的EntityManager,我可以不知道如何检索协会的存储库,这似乎打破了将域与数据库分离的点?
这个问题有推荐的策略吗? / p>
这是我们对他们建议的解决方案的解释。
class Category
{
protected $ id ;
protected $ articles; // PesistentCollection
protected $ em; // EntityManager从某个地方
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);
}
}
接口IArticleRepository
{
function 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.
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);
}
}
- Having entitymanager in an entity isn't a good thing in any case (inject your repository instead)
- 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.
这篇关于Doctine 2限制与DQL的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!