Skip to content

Commit

Permalink
Merge branch 'DoctrineObjectInstructor-Improvements'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Donadeo committed Jan 31, 2014
2 parents 740c63a + 8d6c61a commit e10c174
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,14 @@ public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $d
$identifierList[$name] = $data[$name];
}

// Entity update, load it from database
// Entity update, try to load it from database
$object = $objectManager->find($metadata->name, $identifierList);

if ( ! is_object($object))
{
// Entity with that identifier didn't exist, create a new Entity
return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
}

$objectManager->initializeObject($object);

Expand Down

6 comments on commit e10c174

@victuxbb
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need this change in my project...why you didn't pull request this change?

Thanks!

@cdonadeo
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I wrote a lengthy post asking about this issue, and this repo was so I could have that discussion. I never got an answer, so I'm still not sure if not creating the object is intentional, or a bug.

Since it appears that at least one other person agrees with me on this, I'll make a pull request.

@victuxbb
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found another solution to customize the behaviour...

schmittjoh#333 (comment)

With this you don't need a pull request, using the service alias by config is enought I think...

@cdonadeo
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a similar route when I ran into this. I declared the fixed version you see above as a service in Symfony, then aliased jms_serializer.object_constructor. Example:

<!-- Make JMS Serializer use an alternate object constructor that works with Doctrine. -->
        <service id="mystuff.doctrine_object_constructor" class="MyStuff\CommonBundle\Service\DoctrineObjectConstructor" public="false">
            <argument type="service" id="doctrine" />
            <argument type="service" id="jms_serializer.unserialize_object_constructor" />
        </service>
        <service id="jms_serializer.object_constructor" alias="mystuff.doctrine_object_constructor" public="false" />
        <!-- Done. -->

It hasn't bitten me yet.

@victuxbb
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! well...thinking about this, I believe that the default behaviour should contemplate the creation of an entity with the ID assigned, not only an update.

I agree with you to make a pull request 👍

Thanks!

@magnetik
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also declare your service without having to copy JMS code: https://gist.github.com/magnetik/1815d79e7380c0339e88

Please sign in to comment.