[DDC-1734] Uninitialized proxies cannot be serialized properly Created: 29/Mar/12  Updated: 16/Jan/15  Resolved: 21/Feb/13

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: 2.2
Fix Version/s: 2.4
Security Level: All

Type: Bug Priority: Major
Reporter: Benjamin Morel Assignee: Marco Pivetta
Resolution: Fixed Votes: 1
Labels: None

Issue Links:
is referenced by DDC-3368 [GH-1172] Don't initialize detached p... Resolved


I have the following entity:

class Currency
    /** @Id @Column(type="string") */
    protected $code;
    /** @Column(type="string") */    
    protected $name;

When I serialize an object graph which references a Currency proxy, but this proxy is initialized, then this works as expected:

O:65:"Application\Domain\Proxy\__CG__\Application\Domain\Model\Currency":3:{s:17:"__isInitialized__";b:1;s:7:"*code";s:3:"USD";s:7:"*name";s:20:"United States Dollar";}

However, if the proxy is not initialized, then all properties are serialized as null, and the identity is lost:


That makes a merge() impossible once stored in a session, as the identity of the entity is now unknown.

I think the solution might be:

  • either to have the proxy's $_identity field serialized as well
  • or to have the $code (identity field) populated when the proxy is created.

Comment by Marco Pivetta [ 03/Apr/12 ]

I am personally for dropping the `$_identifier` field and using the entity fields themselves. Will check this tomorrow. (also: not sure if it is possible, just checking)

Comment by Benjamin Eberlei [ 07/Apr/12 ]

This is not fixable. The Proxy requires access to the Entity Persister when unserialized, but that is not available anymore. Proxies are broken if unserialized and then accessed, i believe an exception is thrown in this case.

Comment by Benjamin Morel [ 07/Apr/12 ]

Of course they cannot be initialized once unserialized, but should'nt be merge()able to a new EntityManager anyway?

What I would imagine is:

Comment by Benjamin Morel [ 30/Oct/12 ]

@Marco Pivetta This is a great idea. Have you checked if it is possible?
I'm really surprised that a proxied entity's identity is lost forever upon serialization.

Comment by Matthieu Napoli [ 30/Nov/12 ]

Can this issue be reopened please?

As Benjamin Morel said, it is expected that the proxy is broken once unserialized, but it should be possible to merge to the EntityManager and then use it.

For now, objects graphs with proxies can't be serialized! It could be if the merge would resolve the situation.

I'm giving it a try by serializing the $_identifier field in the Proxy


Comment by Marco Pivetta [ 30/Nov/12 ]

Benjamin Morel you can check my implementation for DCOM-96 at https://github.com/doctrine/common/pull/168 and https://github.com/doctrine/doctrine2/pull/406

Comment by Marco Pivetta [ 30/Nov/12 ]

Matthieu Napoli can you try adding $identifier at https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Proxy/ProxyFactory.php#L319-L323 ?

Comment by Matthieu Napoli [ 30/Nov/12 ]

Marco Pivetta Yes that's what I'm planning to do. I've written a testcase that reproduces the bug right now.

Comment by Marco Pivetta [ 30/Nov/12 ]

Matthieu Napoli awesome. If you can attach the test here, it would be possible to add it to the proxy refactoring.

Comment by Matthieu Napoli [ 30/Nov/12 ]

@ocramius I will make a pull request on Github and post the link here.

However I'm sort of stuck right now: I've added '_identifier' to the fields serialized, but it's not enough. To merge, the UoW needs to initialize the proxy to get the identifiers (because $_identifier is private in the proxy). Problem: $_entityPersister (used to initialize the proxy) is also private.

So I can neither get the identifier of the proxy (even for creating a new proxy for example), nor feed it a new entityPersister to load it... The solution would be to change the proxy a bit (like adding a "__setEntityPersister" method for example) but I guess that changing the Proxy interface in "Doctrine\Common" is a large change and that will not be accepted as a pull request...

Any ideas ?

Comment by Marco Pivetta [ 30/Nov/12 ]

Matthieu Napoli I wouldn't start hacking on that right now... The new impl allows injecting an initializer. A hack may be ok-ish, but the API is already at its EOL.

Comment by Marco Pivetta [ 30/Nov/12 ]

Matthieu Napoli I'd think of marking this as to be fixed in 2.4.x

Comment by Matthieu Napoli [ 30/Nov/12 ]

Marco Pivetta I'm working on the master branch, it's 2.4 right? I don't see anything different for the proxies in this branch (I don't see any way to inject an initializer).

Or is it your PR (https://github.com/doctrine/common/pull/168) maybe?

Comment by Benjamin Morel [ 30/Nov/12 ]

I'm happy that this bug is taken care of, I think that's a major problem that's been dismissed a bit quickly.
Can the bug be reopened, with fix version 2.4, to avoid forgetting it once more?

Comment by Matthieu Napoli [ 30/Nov/12 ]

Benjamin Morel +1

Comment by Marco Pivetta [ 30/Nov/12 ]

Matthieu Napoli yes, that's the PR. Please let me know for the tests so that I can add them to doctrine/common.
I'm re-opening the issue

Comment by Marco Pivetta [ 30/Nov/12 ]

Re-opening issue - need test to verify the behavior before proceeding.

Comment by Matthieu Napoli [ 03/Dec/12 ]

Marco Pivetta Sorry the test was on my computer at work, here it is now: https://gist.github.com/4193721

Just to be clear: I created 2 test methods, the first one works (there is no serialization step), the second doesn't. This is to make it clear that the serialization step is the problem. I hope this is alright.

Comment by Marco Pivetta [ 03/Dec/12 ]

Matthieu Napoli thank you for the test. But please fix the tests so that both fail (if these are actually two verified cases).

Comment by Matthieu Napoli [ 03/Dec/12 ]

Marco Pivetta No those aren't 2 different cases, they are the same, it's just that one fails because we use serialization (this is the problem described in this ticket), and the other one works because we don't use serialization (and it only exists to check that everything works if we don't use serialization). Actually I think the "working test" has no reason to exist really, I just made it to "prove" that the problem was indeed the serialization, so it can be removed.

Comment by Marco Pivetta [ 03/Dec/12 ]

Matthieu Napoli ok, so I can basically strip the working one

Comment by Marco Pivetta [ 06/Jan/13 ]

Matthieu Napoli I deployed a fix in DCOM-96 ( https://github.com/doctrine/doctrine2/pull/406 https://github.com/Ocramius/doctrine2/commit/1424cc781277a4f8183229c5e8d838724e1ca879 )

Comment by Matthieu Napoli [ 15/Jan/13 ]

Marco Pivetta fantastic!

Comment by Benjamin Eberlei [ 25/Jan/13 ]

A related Github Pull-Request [GH-247] was opened

Comment by Benjamin Eberlei [ 26/Jan/13 ]

A related Github Pull-Request [GH-247] was closed

Comment by Marco Pivetta [ 21/Feb/13 ]

Resolved in DCOM-96

Generated at Wed Oct 07 08:33:27 EDT 2015 using JIRA 6.4.10#64025-sha1:5b8b74079161cd76a20ab66dda52747ee6701bd6.