0

I have a Symfony API with two entities : Apartment and User. The apartment entity has a field named owner who is a User object.

private $owner;

I want to create an Appartment linked to the owner with id 8. I send this JSON :

{
"title": "Appartement n°144",
"description": "Description de l'appartement 144",
"rooms": 2,
"area": 11.2,
"price": 101.2,
"visible": true,
"location": {
    "latitude": 0,
    "longitude": 0,
    "altitude": 0
},
"owner": 8

}

I have also tried to change "owner": 8 to "8" or to "owner": {id: "8"}, but I have always this error :

{
"error": {
    "code": 400,
    "message": "Bad Request",
    "exception": [
        {
            "message": "Invalid data \"8\"(integer), expected \"HGB\\CoreBundle\\Entity\\User\".",
            "class": "Symfony\\Component\\HttpKernel\\Exception\\BadRequestHttpException",

If someone has the tips.

Here is my controller :

public function addOneAppartment(Appartment $appartment)
{
    $manager = $this->getDoctrine()->getManager();
    $manager->persist($appartment);
    $manager->flush();

    return $appartment;
}
2
  • Which serializer are you using? Commented Jun 2, 2018 at 15:07
  • The JMS Serializer Commented Jun 2, 2018 at 15:08

1 Answer 1

1

{id: "8"} should work with JMS, make sure you are using the correct groups.

And make sure to use doctrine object constructor:

services:
    jms_serializer.object_constructor:
        alias: jms_serializer.doctrine_object_constructor
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.