G
G
grabbee2019-06-29 22:50:11
symfony
grabbee, 2019-06-29 22:50:11

How to add field to entity Doctrine during serialization?

I use the common FosRestBundle for the site api. It uses the JMS Serializer by default. You need to add a calculated field to the entity Doctrine which is returned via api. This is the URI of the image on the server. The addresses of pictures are not stored in the database, but are generated dynamically by a separate Service.
Previously, I wrote a Listener, connected the Service in it and supplemented the output of the entity with the necessary information

$visitor = $event->getVisitor();
    $visitor->addData('image_thumb', $thumbUrl);

Now they broke everything, and this method does not work. The addData method does not exist . They rewrote everything, there are no docks, there are no manuals on how to rewrite either. How to be then?
With great difficulty I found a description in two lines, but without an explanation of what was happening.
How is such a problem generally solved in this case?
Where to read about JMS Serializer / FosRestBundle changes
gistcomment

https://gist.github.com/svolobuev/ecd931f27982ef6f...
$visitor->addData('image_thumb', $thumbUrl);
is an old version of JMS, now use
$visitor->visitProperty(new StaticPropertyMetadata('', 'image_thumb', null), $thumbUrl);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2019-06-29
@grabbee

Removed deprecated method JsonSerializationVisitor::addData, use :visitProperty(new StaticPropertyMetadata('', 'name', null), 'value') instead

Source: https://github.com/schmittjoh/serializer/blob/mast... In UPGRADING.md , it is
usually written in libraries that they changed from backward compatible ... abstractions - used polymorphism so that you could work more flexibly with serialization / deserialization / groups, etc. to work with an entity property, to control the conditions for adding this field, etc. putting the responsibility on the MetaData object (in pattern language: Strategy) Discussion of the appearance of the solution: https://github.com/schmittjoh/serializer/pull/45

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question