mirror of
https://github.com/doctrine/orm.git
synced 2026-04-27 00:18:04 +02:00
DDC-314: Unserialization data model objects from "empty" string #387
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @doctrinebot on GitHub (Feb 11, 2010).
Jira issue originally created by user cloun:
All of my data model classes contain a specified **weakup method. This method expects that object was serialized with primary key value. And all worked fine.
But begin from the latest revision (7131) class "ClassMetadata" unserializes my objects with invalid serialization data. As I know, Doctrine requires that all classes contain a public contructor without required parameters. Why do you use some kind of hack: unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name))? May be it would be better to use contructor?
@doctrinebot commented on GitHub (Feb 11, 2010):
Comment created by cloun:
I change your code, and it works fine, so far...
@doctrinebot commented on GitHub (Feb 11, 2010):
Comment created by @beberlei:
The JPA requirement of a no-arg constructor is quite a big burdon.
Because PHP allows us, we choose to go the way using unserialize. You can implement the "Serialize" interface and build your logic in there, which plays nicely with our "hack."
See the discussion in DDC-79 for more details, pro- and con arguments on this issue. We will update the documentation shortly reflecting this change.
@doctrinebot commented on GitHub (Feb 11, 2010):
Issue was closed with resolution "Won't Fix"
@doctrinebot commented on GitHub (Feb 11, 2010):
Comment created by romanb:
Calling __wakeup() instead of __construct() on reconstitution of persistent objects is much better in many ways. Calling the constructor is just wrong, can lead to side-effects, slows down instantiation, and more. Please read the comments on DDC-79.
You should implement http://de3.php.net/manual/en/class.serializable.php instead of using __sleep/__wakeup.
@doctrinebot commented on GitHub (Feb 11, 2010):
Comment created by romanb:
Docs updated: http://www.doctrine-project.org/documentation/manual/2_0/en/architecture#entities
@doctrinebot commented on GitHub (Feb 13, 2010):
Comment created by cloun:
I have found simple workaround, may be it will be usefull.
If you have your own implementation of the **weakup method, and you do not want to implement custom serialization methods (to implement ISerializable interface), you can make the next steps:
Where "\0_\0_prototype" is your protected field in base class of all data model classes.
3. Now, you have a flag that indicates whether the object was unserialized as a prototype in depths of the Doctrine or not. So, your *_weakup method should look like below:
@doctrinebot commented on GitHub (Feb 13, 2010):
Comment created by @beberlei:
Can you explain why you need this hack instead of using Serializable?
@doctrinebot commented on GitHub (Feb 13, 2010):
Comment created by romanb:
Modifying Doctrine is usually not a good idea and if you really want to use **wakeup there are other (better) ways to safely use it that are not so intrusive. You say "This method expects that object was serialized with primary key value" so why not just check for that? (see option 1 below).
Here are two methods to safely implement __wakeup. The same could be done for __clone for that matter. We should probably add an entry in the documentation or cookbook about how one can safely implement __wakeup and __clone. I think the simplest way for both is option 1, a simple identity check.
@doctrinebot commented on GitHub (Feb 13, 2010):
Comment created by cloun:
Benjamin Eberlei
May be I'm wrong, but I think that if I implement ISerializable interface then I should write code for custom serialization.
Roman S. Borschel
Yes, you are absolutely right. But as a common solution if you know nothing about customer's model or how to this classes are used I suggested this. Thanks for examples!