@@ -104,6 +104,15 @@ class UnitOfWork implements PropertyChangedListener
104
104
*/
105
105
private $ identityMap = [];
106
106
107
+ /**
108
+ * Associate entities with OIDs to ensure the GC won't recycle a managed entity
109
+ *
110
+ * DDC-2332 / #3037
111
+ *
112
+ * @var array
113
+ */
114
+ private $ oidMap = array ();
115
+
107
116
/**
108
117
* Map of all identifiers of managed entities.
109
118
* Keys are object ids (spl_object_hash).
@@ -1498,7 +1507,8 @@ public function isEntityScheduled($entity)
1498
1507
public function addToIdentityMap ($ entity )
1499
1508
{
1500
1509
$ classMetadata = $ this ->em ->getClassMetadata (get_class ($ entity ));
1501
- $ identifier = $ this ->entityIdentifiers [spl_object_hash ($ entity )];
1510
+ $ oid = spl_object_hash ($ entity );
1511
+ $ identifier = $ this ->entityIdentifiers [$ oid ];
1502
1512
1503
1513
if (empty ($ identifier ) || in_array (null , $ identifier , true )) {
1504
1514
throw ORMInvalidArgumentException::entityWithoutIdentity ($ classMetadata ->name , $ entity );
@@ -1507,6 +1517,7 @@ public function addToIdentityMap($entity)
1507
1517
$ idHash = implode (' ' , $ identifier );
1508
1518
$ className = $ classMetadata ->rootEntityName ;
1509
1519
1520
+ $ this ->oidMap [$ oid ] = $ entity ;
1510
1521
if (isset ($ this ->identityMap [$ className ][$ idHash ])) {
1511
1522
return false ;
1512
1523
}
@@ -1622,6 +1633,7 @@ public function removeFromIdentityMap($entity)
1622
1633
1623
1634
$ className = $ classMetadata ->rootEntityName ;
1624
1635
1636
+ unset($ this ->oidMap [$ oid ]);
1625
1637
if (isset ($ this ->identityMap [$ className ][$ idHash ])) {
1626
1638
unset($ this ->identityMap [$ className ][$ idHash ]);
1627
1639
unset($ this ->readOnlyObjects [$ oid ]);
@@ -2482,6 +2494,7 @@ public function clear($entityName = null)
2482
2494
{
2483
2495
if ($ entityName === null ) {
2484
2496
$ this ->identityMap =
2497
+ $ this ->oidMap =
2485
2498
$ this ->entityIdentifiers =
2486
2499
$ this ->originalEntityData =
2487
2500
$ this ->entityChangeSets =
@@ -2656,6 +2669,7 @@ public function createEntity($className, array $data, &$hints = [])
2656
2669
$ this ->entityStates [$ oid ] = self ::STATE_MANAGED ;
2657
2670
$ this ->originalEntityData [$ oid ] = $ data ;
2658
2671
2672
+ $ this ->oidMap [$ oid ] = $ entity ;
2659
2673
$ this ->identityMap [$ class ->rootEntityName ][$ idHash ] = $ entity ;
2660
2674
2661
2675
if ($ entity instanceof NotifyPropertyChanged) {
@@ -2806,6 +2820,7 @@ public function createEntity($className, array $data, &$hints = [])
2806
2820
// PERF: Inlined & optimized code from UnitOfWork#registerManaged()
2807
2821
$ newValueOid = spl_object_hash ($ newValue );
2808
2822
$ this ->entityIdentifiers [$ newValueOid ] = $ associatedId ;
2823
+ $ this ->oidMap [$ newValueOid ] = $ newValue ;
2809
2824
$ this ->identityMap [$ targetClass ->rootEntityName ][$ relatedIdHash ] = $ newValue ;
2810
2825
2811
2826
if (
0 commit comments