服务对象与Doctrine2和Symfony2 [英] Service objects with Doctrine2 and Symfony2

查看:96
本文介绍了服务对象与Doctrine2和Symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个数据库 A_db 有一个表格

I'm working on a Symfony2/Doctrine2 project which handles 2 databases on MSSqlServer.

我正在处理一个Symfony2 / Doctrine2项目,它处理MSSqlServer上的两个数据库。 em>表单,第二个 B_db 。我所有的实体都用注释定义。

The first database A_db has a table forms and the second one B_db has people. All my entities are defined with annotations.

我需要从 将以下列方式解释。

I need to get all forms from forms related to people as I will explain in following lines.

我花了一些时间阅读相关的回答问题:

I've spent some time reading related answered questions:

  • Most likely what I need Using EntityManager inside Doctrine 2.0 entities!
  • Of course, a service! Doctrine2 Best Practice, Should Entities use Services?
  • Services/Repositories/Whatever Using the Data Mapper Pattern, Should the Entities (Domain Objects) know about the Mapper?

所以我决定服务可能是处理我的需求的最佳方式。但是,对于我来说,实际上如何做到这一点并不清楚。我的意思是,在哪里放我的服务类,如何定义在config.yml,如何进入 实体...

So I decided that a service might be the best way to handle my needs. But it makes no clear to me how actually get this done. I mean, where to put my service class, how to define it in config.yml, how into people entity...

我的愿望要设置一个完整的服务(假设它是最好的实现)来执行如下操作:

My wish is to set up a full service (assuming its the best implementation) to perform something like:

foreach($onePeple->getForms() as $form) {/* some code with form */}

如果服务实现对于教义不是最好的做法,那么它会是什么,我该如何使它工作?

In case service implementation for doctrine is not the best practice, then what would it be and how can I make it work?

推荐答案

你是什么要求可以单独使用这些实体 - 只要在Form实体上定义一个这样的关系:

What you're asking there is possible using the entities alone - so long as you define a relationship such as this on the Form entity:

/**
 * @OneToMany(targetEntity="Form", mappedBy="User")
 */
protected $Forms;

在用户实体上:

/**
 * @ManyToOne(targetEntity="User", inversedBy="Forms")
 */
protected $User;

然后你可以简单地加载一个用户实体(通过一个服务,一个存储库,或者你想要的),然后通过 $ userObj-> Forms (如果在您的实体上使用magic __get)访问所有属于该用户的表单,如果不是,则使用getter方法,再次下载到偏爱)。 表单是实现 Doctrine\Common\Collections\Collection 接口的对象的实例(因为它是一个很多的关系),它可以使用foreach进行迭代。

Then you can simply load a User entity (through a service, a repository or however you wish) and then access all the forms belonging to that user through $userObj->Forms (if using magic __get on your entities, if not then using a getter method, again down to your preference). Forms is an instance of an object implementing the Doctrine\Common\Collections\Collection interface (since it is a to-many relationship) which is iterable using a foreach.

在我使用Doctrine2的项目中,我们通常使用我们的服务来获取,保存和删除实体,以及一个类型的列表实体(我们称之为索引方法)。这样,您可以绑定保存所需的额外功能,例如更新与其密切相关的其他实体等。这些服务与持久层相关,并包含对实体管理员的引用,使实体本身可以隔离和可测试。

In projects I have worked on using Doctrine2, we typically use our services for getting, saving and deleting entities, along with listing entities of a type (we call them index methods). This way you can tie in extra functionality required in saving, such as updating other entities which are closely associated and so on. These services are tied to the persistence layer and contain a reference to the entity manager, leaving the entities themselves isolated and testable.

我们也有论述是否将这种逻辑放在知识库或服务中,但这更多是个人偏好和/或项目需求的情况。

We've also had the argument of whether to put this kind of logic in repositories or services, but that is more a case of personal preference and/or project requirements.

有一件事要注意 - 你建立的服务不是要与教义联系起来。它不知道您的服务层(它只是一些用户界面代码),所以由您来连接它有意义和干净的方式。实质上,您需要一些可以通过服务构造函数传递实体管理器的东西,并具有能够对所有实体进行操作的某种基本服务类,然后可以在每个实体的基础上扩展特殊逻辑。然而,存储库本质上与Doctrine相关联,所以如果这是你想要的,那么它可能是最好的解决方案。我们倾向于使用服务,因为它们纯粹与实现业务规则等相关,将存储库用作持久层的组件。

One thing to note - services you build are not going to be linked into Doctrine. It is unaware of your service layer (it's just some userland code) so it is up to you to link it in a meaningful and clean manner. Essentially you'd want something where you could pass the entity manager in through the constructor of the service and have some kind of base service class capable of operating on all entities, which could then be extended with special logic on a per-entity basis. Repositories, however, are intrinsically linked into Doctrine, so if that is what you want then it might be the best solution. We tended to use services as they are then purely related to implementing the business rules and so on, leaving the repositories to be used as a component of the persistence layer.

这篇关于服务对象与Doctrine2和Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆