Reputation: 1286
I have ManyToOne relationship between Posts and User:
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User\User")
*/
private $author;
After I obtain response form my endpoint which looks like this:
array:2 [
"message" => "OK"
"data" => array:1 [
0 => Post {#6077
-id: 1
-title: "sda"
-teaser: "asdj"
-content: "asd"
-createdDateTime: DateTime @1517078058 {#6075
date: 2018-01-27 18:34:18.0 UTC (+00:00)
}
-deletedDateTime: null
-author: User {#6294
+__isInitialized__: false
-id: 1
-firstName: null
-lastName: null
-email: null
-activationCode: null
-isActive: null
-createdDateTime: null
-deletedDateTime: null
…2
}
-statusId: false
}
]
]
I'm trying to serialize it to JSON. Perviously(Symfony 3) there was a problem with Circular References which I managed to solve but this is little bit different.
I keep getting 500 response with exception:
NotNormalizableValueException
An unexpected value could not be normalized: NULL
If I remove relationship between entities it's fine so it's clearly the problem.
It's treating related object as something that cannot be normalized. For some reason. Not sure why it's happening. Didn't find anything in the docs on that.
Anything I missed here?
Upvotes: 1
Views: 3582
Reputation: 1286
I think I have solved my problem. I went to:
/vendor/symfony/serializer/Serializer.php
After little investigation I was able to find out that normalizer is falling over PHP RESOURCE.
It turned out that I had one field of VARBINARY type in my User table. According to doctrine documentation that type is translated to PHP resource.
Conclusion: Symfony serialization does not have any problems with serializing entity and related child entities. It is simply not capable of normalizing resource type of data.
I think that's something worth remembering.
Upvotes: 2
Reputation: 1828
I would need some more info to help you. What serializer are you using, the Symfony one? How are you loading the serializer? Whatever it may be, the error is clear: the normalizer of the serializer you are using does not know what to do when finds a null field. My guess is that you are loading just one normalizer (of the around 5 available) to your serializer as it is shown in the official Symfony Serializer docs.
Upvotes: 1