[英]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.