Skip to content

Commit 1fec1cb

Browse files
committed
Use Doctrine Instantiator for ClassMetadata new instance creation
As mentioned in the changelog, __clone() methods will no longer be invoked. This is a BC break for code that might have relied upon that behavior.
1 parent ada5971 commit 1fec1cb

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

CHANGELOG-1.0.md

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ To generate a changelog summary since the last version, run
1515
1.0.x-dev
1616
---------
1717

18+
#### `__clone()` method no longer called when documents are instantiated via `ClassMetadata::newInstance()`
19+
20+
As of [#956](https://github.com/doctrine/mongodb-odm/pull/956), ClassMetadata
21+
uses the [Doctrine Instantiator](https://github.com/doctrine/instantiator)
22+
library to create new document instances. This library avoids calling
23+
`__clone()` or any public API on instantiated objects. This is a BC break for\
24+
code that may have relied upon the previous behavior.
25+
1826
1.0.0-BETA11 (2014-06-06)
1927
-------------------------
2028

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"doctrine/common": "2.4.*",
2020
"doctrine/cache": "~1.0",
2121
"doctrine/inflector": "~1.0",
22+
"doctrine/instantiator": "~1.0.1",
2223
"doctrine/mongodb": ">=1.1.5,<2.0"
2324
},
2425
"require-dev": {

lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php

+6-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace Doctrine\ODM\MongoDB\Mapping;
2121

22+
use Doctrine\Instantiator\Instantiator;
2223
use Doctrine\ODM\MongoDB\LockException;
2324

2425
/**
@@ -49,11 +50,9 @@ class ClassMetadata extends ClassMetadataInfo
4950
public $reflFields = array();
5051

5152
/**
52-
* The prototype from which new instances of the mapped class are created.
53-
*
54-
* @var object
53+
* @var \Doctrine\Instantiator\InstantiatorInterface|null
5554
*/
56-
private $prototype;
55+
private $instantiator;
5756

5857
/**
5958
* Initializes a new ClassMetadata instance that will hold the object-document mapping
@@ -67,6 +66,7 @@ public function __construct($documentName)
6766
$this->reflClass = new \ReflectionClass($documentName);
6867
$this->namespace = $this->reflClass->getNamespaceName();
6968
$this->setCollection($this->reflClass->getShortName());
69+
$this->instantiator = new Instantiator();
7070
}
7171

7272
/**
@@ -176,6 +176,7 @@ public function __wakeup()
176176
{
177177
// Restore ReflectionClass and properties
178178
$this->reflClass = new \ReflectionClass($this->name);
179+
$this->instantiator = $this->instantiator ?: new Instantiator();
179180

180181
foreach ($this->fieldMappings as $field => $mapping) {
181182
if (isset($mapping['declared'])) {
@@ -195,12 +196,6 @@ public function __wakeup()
195196
*/
196197
public function newInstance()
197198
{
198-
if ($this->prototype === null) {
199-
$this->prototype = version_compare(PHP_VERSION, '5.4.0', '>=')
200-
? $this->reflClass->newInstanceWithoutConstructor()
201-
: unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
202-
}
203-
204-
return clone $this->prototype;
199+
return $this->instantiator->instantiate($this->name);
205200
}
206201
}

0 commit comments

Comments
 (0)