Can symfony serializer deserialize return nested entity of type child entity? Can symfony serializer deserialize return nested entity of type child entity? symfony symfony

Can symfony serializer deserialize return nested entity of type child entity?


I believe the Symfony serializer attempts to be minimal compared to the JMS Serializer, so you might have to implement your own denormalizer for the class. You can see how the section on adding normalizers.


There may be an easier way, but so far with Symfony I am using Discriminator interface annotation and type property for array of Objects. It can also handle multiple types in one array (MongoDB):

namespace App\Model;use Symfony\Component\Serializer\Annotation\DiscriminatorMap;/** * @DiscriminatorMap(typeProperty="type", mapping={ *    "text"="App\Model\BlogContentTextModel", *    "code"="App\Model\BlogContentCodeModel" * }) */interface BlogContentInterface{    /**     * @return string     */    public function getType(): string;}

and parent object will need to define property as interface and get, add, remove methods:

    /**     * @var BlogContentInterface[]     */    protected $contents = [];        /**     * @return BlogContentInterface[]     */    public function getContents(): array    {        return $this->contents;    }    /**     * @param BlogContentInterface[] $contents     */    public function setContents($contents): void    {        $this->contents = $contents;    }    /**     * @param BlogContentInterface $content     */    public function addContent(BlogContentInterface $content): void    {        $this->contents[] = $content;    }    /**     * @param BlogContentInterface $content     */    public function removeContent(BlogContentInterface $content): void    {        $index = array_search($content, $this->contents);        if ($index !== false) {            unset($this->contents[$index]);        }    }