Doctrine 2 限制与 DQL 的关联

2023-08-18php开发问题
3

本文介绍了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);
    }
}

推荐答案

  1. 在实体中使用 entitymanager 在任何情况下都不是一件好事(改为注入您的存储库)
  2. 类别不是文章的唯一根,因为它无法确定您需要哪些文章,因此您需要一个文章存储库.

我会怎么做:

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 的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17