A
A
ak_wi2020-10-02 21:57:24
symfony
ak_wi, 2020-10-02 21:57:24

JMS serializer how to deserialize ManyToOne?

An entity has a property:

/**
     * @JMS\Type("App\Entity\Client")
     *
     * @ORM\ManyToOne(targetEntity=Client::class, inversedBy="orders")
     */
    private $client;

How to set up JMS so that it can be passed in json:
{"clientId": 2}

and by this id pull up the client entity after serialization?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2020-10-06
@ak_wi

Alternatively, make a method that returns the id of the client and assign a VirtualProperty to it:

/**
     * @JMS\VirtualProperty
     * @JMS\SerializedName("clientId")
     */
    public function getClientId()
    {
        return $this->client->getId();
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question