You are browsing a version that is no longer maintained. |
Implementing Wakeup or Clone
As explained in the
restrictions for document classes in the manual.
it is usually not allowed for a document to implement __wakeup
or __clone
, because Doctrine makes special use of them.
However, it is quite easy to make use of these methods in a safe
way by guarding the custom wakeup or clone code with a document
identity check, as demonstrated in the following sections.
Safely implementing __wakeup
To safely implement __wakeup
, simply enclose your
implementation code in an identity check as follows:
1 <?php
class MyDocument
{
private $id; // This is the identifier of the document.
//...
public function __wakeup()
{
// If the document has an identity, proceed as normal.
if ($this->id !== null) {
// ... Your code here as normal ...
}
// otherwise do nothing, do NOT throw an exception!
}
//...
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Safely implementing __clone
Safely implementing __clone
is pretty much the same:
1 <?php
class MyDocument
{
private $id; // This is the identifier of the document.
//...
public function __clone()
{
// If the document has an identity, proceed as normal.
if ($this->id !== null) {
// ... Your code here as normal ...
}
// otherwise do nothing, do NOT throw an exception!
}
//...
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Summary
As you have seen, it is quite easy to safely make use of
__wakeup
and __clone
in your documents without adding any
really Doctrine-specific or Doctrine-dependant code.
These implementations are possible and safe because when Doctrine invokes these methods, the documents never have an identity (yet). Furthermore, it is possibly a good idea to check for the identity in your code anyway, since it's rarely the case that you want to unserialize or clone a document with no identity.