php - Why the need for safe wakeup and clone methods in doctrine 2? -


the documentation warns few times ensuring wakeup , clone implementations on doctrine 2 entities, e.g.:

http://docs.doctrine-project.org/en/latest/cookbook/implementing-wakeup-or-clone.html

it doesn't why, however... presume has not messing doctrine's entity manager , in-memory-cache might maintain, cursory dive (enormous) code base doesn't more, nor did quick search of users list.

might knowledgeable doctrine internals know precisely why doctrine 2 needs safe wakeup , clone methods on entities?

doctrine 2 needs convert data fetched database entities (php objects). process called hydration.

doctrine 2 doesn't use traditional method of using new operator (thus constructing class), uses following method:

  • it builds string represents serialized empty version of class.
  • it unserializes string make actual object. why need safely implement __wakeup.
  • it stores object prototype.

this process done once each entity class.

then whenever needs hydrate entity, clone prototype. why need safely implement __clone.

this done small method newinstance() in classmetadatainfo.

the advantage of doing constructor isn't used when new entity hydrated. leaves developer free he/she wants in constructor (including using parameters).

update

the reason why need safely implement __wakeup , __clone methods this:

because doctrine 2 unserializes , clones entities when needs hydrate them, methods called. when doctrine 2 this, entities not have data set (not identifier(s)). data set afterwards.

so when implement them in such way logic performed when entity does have identifier(s), sure logic performed when you unserialize or clone entity, not when doctrine 2 it.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -