S
S
startproger2019-11-07 21:04:55
Doctrine ORM
startproger, 2019-11-07 21:04:55

How to set additional properties to entities in Doctrine ORM?

Hello! Before I used any ORM, I crutched my models, and if after receiving, for example, publication, from the model, in the controller, if I need to set additional properties for it (necessary only in the controller and view, for example, a link), then and wrote: $post->link = /blog/.$post->id
Now I use Doctrine ORM, in which properties should be private, and setters / getters for such things (which are not entered into the database, only the controller and view) don't have to do.
Gentlemen and ladies, what is the best way to manage in these cases, if you need to add any properties like link and something else that I might need to each object for use in the controller and view?
ps I use Doctrine alone without Symfony in my bike.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2019-11-07
@startproger

Now I use Doctrine ORM, whose properties should be private, and setters / getters for such things ( which are not entered into the database , only the controller and the view) do not need to be done.

The doctrine is purple for the privacy / openness of your properties, exactly as for the presence of methods for
writing / reading them, besides this, setters / getters are common (absolutely everywhere), but still this is a sign of bad code, I wrote an article about this on Habré: Getters / setters and the encapsulation problem .
And so the Doctrine works with objects through reflection.
How to display the data that is needed in another layer, which is needed for display, and not for business logic - DTO,
answered the other day: What if you need to get part of the entity data?
If you want to work with an entity field, but do not want it to be loaded from / to the database, then simply do not make an annotation for this field (or do not specify it in yml if you use it for mapping):
Marks an annotated instance variable as "persistent". It has to be inside the instance variables PHP DocBlock comment. Any value hold inside this variable will be saved to and loaded from the database as part of the lifecycle of the instance variables entity-class.
https://www.doctrine-project.org/projects/doctrine...
I recommend either a DTO or a service (as BoShurik pointed out ) that will return a link, while you can feed a router to this service and create a link not by the name of the entity, but by the name of the route - this will make it easier later to change all routes of the same type at once, if not change the name. And generally both DTO and service are necessary. In any case, the entity should not know anything about routes, especially since there can be much more of these routes for one entity, /blog/and this can change quite often ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question