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