3838use Doctrine \ORM \Persisters \Entity \EntityPersister ;
3939use Doctrine \ORM \Persisters \Entity \JoinedSubclassPersister ;
4040use Doctrine \ORM \Persisters \Entity \SingleTablePersister ;
41+ use Doctrine \ORM \Proxy \InternalProxy ;
4142use Doctrine \ORM \Utility \IdentifierFlattener ;
4243use Doctrine \Persistence \Mapping \RuntimeReflectionService ;
4344use Doctrine \Persistence \NotifyPropertyChanged ;
4445use Doctrine \Persistence \ObjectManagerAware ;
4546use Doctrine \Persistence \PropertyChangedListener ;
46- use Doctrine \Persistence \Proxy ;
4747use Exception ;
4848use InvalidArgumentException ;
4949use RuntimeException ;
@@ -581,7 +581,7 @@ private function computeSingleEntityChangeSet($entity): void
581581 }
582582
583583 // Ignore uninitialized proxy objects
584- if ($ entity instanceof Proxy && ! $ entity -> __isInitialized ( )) {
584+ if ($ this -> isUninitializedObject ( $ entity )) {
585585 return ;
586586 }
587587
@@ -906,7 +906,7 @@ public function computeChangeSets()
906906
907907 foreach ($ entitiesToProcess as $ entity ) {
908908 // Ignore uninitialized proxy objects
909- if ($ entity instanceof Proxy && ! $ entity -> __isInitialized ( )) {
909+ if ($ this -> isUninitializedObject ( $ entity )) {
910910 continue ;
911911 }
912912
@@ -931,7 +931,7 @@ public function computeChangeSets()
931931 */
932932 private function computeAssociationChanges (array $ assoc , $ value ): void
933933 {
934- if ($ value instanceof Proxy && ! $ value -> __isInitialized ( )) {
934+ if ($ this -> isUninitializedObject ( $ value )) {
935935 return ;
936936 }
937937
@@ -2172,7 +2172,7 @@ private function ensureVersionMatch(
21722172 $ entity ,
21732173 $ managedCopy
21742174 ): void {
2175- if (! ($ class ->isVersioned && $ this ->isLoaded ($ managedCopy ) && $ this ->isLoaded ($ entity ))) {
2175+ if (! ($ class ->isVersioned && ! $ this ->isUninitializedObject ($ managedCopy ) && ! $ this ->isUninitializedObject ($ entity ))) {
21762176 return ;
21772177 }
21782178
@@ -2190,16 +2190,6 @@ private function ensureVersionMatch(
21902190 throw OptimisticLockException::lockFailedVersionMismatch ($ entity , $ entityVersion , $ managedCopyVersion );
21912191 }
21922192
2193- /**
2194- * Tests if an entity is loaded - must either be a loaded proxy or not a proxy
2195- *
2196- * @param object $entity
2197- */
2198- private function isLoaded ($ entity ): bool
2199- {
2200- return ! ($ entity instanceof Proxy) || $ entity ->__isInitialized ();
2201- }
2202-
22032193 /**
22042194 * Sets/adds associated managed copies into the previous entity's association field
22052195 *
@@ -2495,7 +2485,7 @@ static function ($assoc) {
24952485 */
24962486 private function cascadePersist ($ entity , array &$ visited ): void
24972487 {
2498- if ($ entity instanceof Proxy && ! $ entity -> __isInitialized ( )) {
2488+ if ($ this -> isUninitializedObject ( $ entity )) {
24992489 // nothing to do - proxy is not initialized, therefore we don't do anything with it
25002490 return ;
25012491 }
@@ -2569,13 +2559,13 @@ static function ($assoc) {
25692559 }
25702560 );
25712561
2562+ if ($ associationMappings ) {
2563+ $ this ->initializeObject ($ entity );
2564+ }
2565+
25722566 $ entitiesToCascade = [];
25732567
25742568 foreach ($ associationMappings as $ assoc ) {
2575- if ($ entity instanceof Proxy && ! $ entity ->__isInitialized ()) {
2576- $ entity ->__load ();
2577- }
2578-
25792569 $ relatedEntities = $ class ->reflFields [$ assoc ['fieldName ' ]]->getValue ($ entity );
25802570
25812571 switch (true ) {
@@ -2631,9 +2621,7 @@ public function lock($entity, int $lockMode, $lockVersion = null): void
26312621 return ;
26322622 }
26332623
2634- if ($ entity instanceof Proxy && ! $ entity ->__isInitialized ()) {
2635- $ entity ->__load ();
2636- }
2624+ $ this ->initializeObject ($ entity );
26372625
26382626 assert ($ class ->versionField !== null );
26392627 $ entityVersion = $ class ->reflFields [$ class ->versionField ]->getValue ($ entity );
@@ -2823,7 +2811,6 @@ public function createEntity($className, array $data, &$hints = [])
28232811 $ unmanagedProxy = $ hints [Query::HINT_REFRESH_ENTITY ];
28242812 if (
28252813 $ unmanagedProxy !== $ entity
2826- && $ unmanagedProxy instanceof Proxy
28272814 && $ this ->isIdentifierEquals ($ unmanagedProxy , $ entity )
28282815 ) {
28292816 // We will hydrate the given un-managed proxy anyway:
@@ -2832,7 +2819,8 @@ public function createEntity($className, array $data, &$hints = [])
28322819 }
28332820 }
28342821
2835- if ($ entity instanceof Proxy && ! $ entity ->__isInitialized ()) {
2822+ if ($ this ->isUninitializedObject ($ entity )) {
2823+ assert ($ entity instanceof InternalProxy);
28362824 $ entity ->__setInitialized (true );
28372825 } else {
28382826 if (
@@ -2978,8 +2966,7 @@ public function createEntity($className, array $data, &$hints = [])
29782966 $ hints ['fetchMode ' ][$ class ->name ][$ field ] === ClassMetadata::FETCH_EAGER &&
29792967 isset ($ hints [self ::HINT_DEFEREAGERLOAD ]) &&
29802968 ! $ targetClass ->isIdentifierComposite &&
2981- $ newValue instanceof Proxy &&
2982- $ newValue ->__isInitialized () === false
2969+ $ this ->isUninitializedObject ($ newValue )
29832970 ) {
29842971 $ this ->eagerLoadingEntities [$ targetClass ->rootEntityName ][$ relatedIdHash ] = current ($ associatedId );
29852972 }
@@ -3377,7 +3364,7 @@ public function registerManaged($entity, array $id, array $data)
33773364
33783365 $ this ->addToIdentityMap ($ entity );
33793366
3380- if ($ entity instanceof NotifyPropertyChanged && ( ! $ entity instanceof Proxy || $ entity -> __isInitialized () )) {
3367+ if ($ entity instanceof NotifyPropertyChanged && ! $ this -> isUninitializedObject ( $ entity )) {
33813368 $ entity ->addPropertyChangedListener ($ this );
33823369 }
33833370 }
@@ -3485,7 +3472,7 @@ public function getScheduledCollectionUpdates()
34853472 */
34863473 public function initializeObject ($ obj )
34873474 {
3488- if ($ obj instanceof Proxy ) {
3475+ if ($ obj instanceof InternalProxy ) {
34893476 $ obj ->__load ();
34903477
34913478 return ;
@@ -3496,6 +3483,16 @@ public function initializeObject($obj)
34963483 }
34973484 }
34983485
3486+ /**
3487+ * Tests if a value is an uninitialized entity.
3488+ *
3489+ * @param mixed $obj
3490+ */
3491+ public function isUninitializedObject ($ obj ): bool
3492+ {
3493+ return $ obj instanceof InternalProxy && ! $ obj ->__isInitialized ();
3494+ }
3495+
34993496 /**
35003497 * Helper method to show an object as string.
35013498 *
@@ -3646,13 +3643,11 @@ private function assertThatThereAreNoUnintentionallyNonPersistedAssociations():
36463643 */
36473644 private function mergeEntityStateIntoManagedCopy ($ entity , $ managedCopy ): void
36483645 {
3649- if (! $ this ->isLoaded ($ entity )) {
3646+ if ($ this ->isUninitializedObject ($ entity )) {
36503647 return ;
36513648 }
36523649
3653- if (! $ this ->isLoaded ($ managedCopy )) {
3654- $ managedCopy ->__load ();
3655- }
3650+ $ this ->initializeObject ($ managedCopy );
36563651
36573652 $ class = $ this ->em ->getClassMetadata (get_class ($ entity ));
36583653
@@ -3673,7 +3668,7 @@ private function mergeEntityStateIntoManagedCopy($entity, $managedCopy): void
36733668 if ($ other === null ) {
36743669 $ prop ->setValue ($ managedCopy , null );
36753670 } else {
3676- if ($ other instanceof Proxy && ! $ other -> __isInitialized ( )) {
3671+ if ($ this -> isUninitializedObject ( $ other )) {
36773672 // do not merge fields marked lazy that have not been fetched.
36783673 continue ;
36793674 }
0 commit comments