Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make deserialisation of non existing id's configurable #333

Closed
whitewhidow opened this issue Sep 6, 2014 · 2 comments
Closed

make deserialisation of non existing id's configurable #333

whitewhidow opened this issue Sep 6, 2014 · 2 comments

Comments

@whitewhidow
Copy link

Hi,

please look at this:

cdonadeo@e10c174

this person needs an implementation, where non-existing ids, become new entities

I need a different implementation, wich would ignore non-existing id's, and set the relation to null,

i did this by changing the following:

+++ /home/sam/git/badge/vendor/jms/serializer/src/JMS/Serializer/GenericDeserializationVisitor.php
@@ -172,6 +172,10 @@

         $v = $data[$name] !== null ? $this->navigator->accept($data[$name], $metadata->type, $context) : null;

+        if (is_null($this->currentObject)){
+            return;
+        }
+
         if (null === $metadata->setter) {
             $metadata->reflection->setValue($this->currentObject, $v);

now my question is, would it not be usefull to be able to configure the serializer via the config, to either,

try either try to create the non-existing resource, as cdonadeo did, or ignore it, like i did.

instead of the current behaviour, wich causes an exception when trying to deserialize an entity with an id in it, that is not present in the database

Please let me know if there is anything i can do to make this clearer

Thx

EDIT:

after re-reading this, i realize, that my questions regarding beeing able to config this trough the config, belong in the repo for the bundle, but at the same time im assuming it would require changes in both repos for this to be done ? correct?

@victuxbb
Copy link

Hi! you know something about this? There is a pull request about that?

The commit of cdonadeo/serializer@e10c174 is exactly what I need but I see that there isn't a pull request...

Thanks!

@victuxbb
Copy link

ok....I found another solution...
You can define your own DoctrineObjectConstructor creating a service with your class and implementing ObjectConstructorInterface, then you can customize the behaviour.

services:
    jms_serializer.object_constructor:
        alias: custom.doctrine_object_constructor
        public: false
    custom.doctrine_object_constructor:
        class: Vendor\YourCustomBundle\Serializer\Construction\DoctrineObjectConstructor
        arguments: ['@doctrine','@jms_serializer.unserialize_object_constructor']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants