简体   繁体   English

从Doctrine2存储库中获取关联实体集合作为数组

[英]Fetch associated entities collection from Doctrine2 repository as an array

I'm starting a little project with DDD approach. 我正在使用DDD方法开始一个小项目。 I've created my domain model with Entities and ValueObjects in plain PHP. 我已经在纯PHP中使用Entities和ValueObjects创建了域模型。 Entities have references to their associations - in my case, there is an Employee entity with collection of Teams he belongs to, and I keep them in Employee::teams property. 实体引用了它们的关联-在我的例子中,有一个Employee实体,其中包含他所属的Teams ,我将它们保留在Employee::teams属性中。 Everything is going great, I've created mappings for those entities with associations in YAML, interfaces for repositories to be implemented in the Symfony2 and Doctrine2 layer, etc. 一切进展顺利,我已经为那些具有YAML中的关联的实体,在Symfony2和Doctrine2层中实现的存储库的接口等创建了映射。

When I fetch Employee s from repository (with Doctrine's EntityManager::findAll() ) instead of array of Team s I receive PersistentCollection with those teams. 当我从存储库(使用Doctrine的EntityManager::findAll() )而不是Team数组获取Employee ,我收到了这些团队的PersistentCollection It's built on PHP7, and Employee::getTeams() has return type of array so I'm getting the critical exception. 它基于PHP7构建,并且Employee::getTeams()具有array返回类型,因此我遇到了严重的异常。 Is there any way to convert it into array with some external listeners (Symfony layer only, to not to mess in domain files) or any other core mechanism? 是否可以通过某些外部侦听器(仅Symfony层,不要将其混入域文件)或其他任何核心机制将其转换为数组?

Thanks. 谢谢。

You get ArrayCollection 您得到ArrayCollection
http://www.doctrine-project.org/api/common/2.5/class-Doctrine.Common.Collections.ArrayCollection.html http://www.doctrine-project.org/api/common/2.5/class-Doctrine.Common.Collections.ArrayCollection.html

It has toArray() method so you can write 它具有toArray()方法,因此您可以编写

/**
 * @return Team[]
 */
public function getTeams(): array
{
    return $this->teams->toArray();
}

But I prefer 但是我更喜欢

/**
 * @return Team[]|ArrayCollection
 */
public function getTeams()
{
    return $this->teams;
}

If you use good IDE like PhpStorm it will understand it correctly to autocomplete it like ArrayCollection and as Team if you make foreach ($employee->getTeams() as $team) {... 如果你使用好的IDE像PhpStorm它将正确理解它自动完成它像的ArrayCollection和Team ,如果你做foreach ($employee->getTeams() as $team) {...

ArrayCollection are more powerful than plain arrays. ArrayCollection比普通数组更强大。 For example you can filter and order them and Doctrine will make optimized SQL so you don't have to load all items 例如,您可以对其进行过滤和排序,Doctrine将生成优化的SQL,因此您不必加载所有项目
http://docs.doctrine-project.org/en/latest/reference/working-with-associations.html#filtering-collections http://docs.doctrine-project.org/en/latest/reference/working-with-associations.html#filtering-collections

Kuba, 库巴

That doesn't seem to be a DDD question but a technical question regarding PHP implementation details. 这似乎不是DDD问题,而是有关PHP实现细节的技术问题。 However, please let me jump into your design and try to find a better solution for your domain. 但是,请让我跳入您的设计并尝试为您的领域找到更好的解决方案。 You are missing to model the relation between an employee and a team, let's call it employee in team. 您缺少建模员工与团队之间关系的模型,我们称其为团队中的员工。 The way you did you are forcing an AR to reference another AR, saying this way that employee manages a team life cycle. 您的工作方式是迫使一个AR引用另一个AR,这样员工可以管理团队的生命周期。 This might not be the case but under a specific scenario another actor might change a status of a team and that could break the Employee invariant over the Team AR. 可能并非如此,但在特定情况下,另一个参与者可能会更改团队的状态,这可能会破坏员工对团队AR的不变性。 Because of this when you make an AR to manage another AR life cycle then this dependency shouldn't be found through the repository and the root of the aggregate is in the position to now force invariant around the aggregated AR. 因此,当您使一个AR管理另一个AR生命周期时,就不应在存储库中找到这种依赖性,并且聚合的根现在可以迫使聚合AR保持不变。 There are still some concerns to mention, but just to keep it simple, go ahead and model the concept you are missing: EmployeeInTeam or whatever you want to call it. 仍然有一些需要提及的问题,但是为了简单起见,请继续对缺少的概念进行建模:EmployeeInTeam或任何您想调用的概念。 It references the conceptual identity of the employee and the team so you can remove it safely in a future, you can query employees in a team and teams an employee is part of. 它引用了员工和团队的概念性身份,因此您将来可以安全地将其删除,可以查询团队中的员工以及该员工所在的团队。 If you need frameworks or the DB to keep the consistence then you are not doing DDD. 如果您需要框架或数据库来保持一致性,那么您就无需执行DDD。 Use just objects, not the technology. 仅使用对象,而不是技术。

Regards, Sebastian. 问候,塞巴斯蒂安。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM