Doctrine 2 Restricting Associations with DQL(Doctrine 2 限制与 DQL 的关联)
问题描述
在 Doctrine 2.1 中似乎有一个问题,返回子集并不容易为协会收集.
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#restricting-associations
文档建议编写存储库查找方法,这是有道理的,因为这是我想做的第一件事.
The docs recommend to write a repository find method, which makes sense because that was the first thing I though of doing.
但是,如果没有对实体内的 EntityManager 的引用,我看不出您将如何检索关联的存储库,这似乎违背了将域与数据库分离的意义?
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);
}
}
推荐答案
- 在实体中使用 entitymanager 在任何情况下都不是一件好事(改为注入您的存储库)
- 类别不是文章的唯一根,因为它无法确定您需要哪些文章,因此您需要一个文章存储库.
我会怎么做:
class Category
{
protected $id;
protected $articles; // PesistentCollection
public function getVisableArticles(IArticleRepository $articleRepository)
{
return $articleRepository->getVisibleByCategory($this);
}
}
interface IArticleRepository
{
function getVisibleByCategory(Category $category);
}
您的学说的存储库将实现 IArticleRepository,而该类将不知道您的数据存储/学说的任何信息.
Your doctrine's repository would implement IArticleRepository and the class won't know anything about your data storage/doctrine.
这篇关于Doctrine 2 限制与 DQL 的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Doctrine 2 限制与 DQL 的关联


- PHP foreach() 与数组中的数组? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01