Service objects with Doctrine2 and Symfony2

2019-04-11 15:57发布

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

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:

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?

1条回答
对你真心纯属浪费
2楼-- · 2019-04-11 16:23

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;

And on the User entity:

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

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.

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.

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.

查看更多
登录 后发表回答