1212use Doctrine \Persistence \NotifyPropertyChanged ;
1313use LogicException ;
1414use ReflectionClass ;
15+ use ReflectionProperty ;
1516use WeakMap ;
1617
18+ use function array_key_exists ;
1719use function count ;
1820
1921use const PHP_VERSION_ID ;
@@ -27,6 +29,9 @@ class NativeLazyObjectFactory implements ProxyFactory
2729 private readonly UnitOfWork $ unitOfWork ;
2830 private readonly LifecycleEventManager $ lifecycleEventManager ;
2931
32+ /** @var array<class-string, ReflectionProperty[]> */
33+ private array $ skippedProperties = [];
34+
3035 public function __construct (
3136 DocumentManager $ documentManager ,
3237 ) {
@@ -68,13 +73,36 @@ public function getProxy(ClassMetadata $metadata, $identifier): object
6873
6974 $ metadata ->propertyAccessors [$ metadata ->identifier ]->setValue ($ proxy , $ identifier );
7075
76+ foreach ($ this ->getSkippedProperties ($ metadata ) as $ property ) {
77+ $ property ->skipLazyInitialization ($ proxy );
78+ }
79+
7180 if (isset (self ::$ lazyObjects )) {
7281 self ::$ lazyObjects [$ proxy ] = true ;
7382 }
7483
7584 return $ proxy ;
7685 }
7786
87+ /** @return ReflectionProperty[] */
88+ private function getSkippedProperties (ClassMetadata $ metadata ): array
89+ {
90+ if (isset ($ this ->skippedProperties [$ metadata ->name ])) {
91+ return $ this ->skippedProperties [$ metadata ->name ];
92+ }
93+
94+ $ skippedProperties = [];
95+ foreach ($ metadata ->reflClass ->getProperties () as $ property ) {
96+ if (array_key_exists ($ property ->name , $ metadata ->propertyAccessors )) {
97+ continue ;
98+ }
99+
100+ $ skippedProperties [] = $ property ;
101+ }
102+
103+ return $ this ->skippedProperties [$ metadata ->name ] = $ skippedProperties ;
104+ }
105+
78106 /** @internal Only for tests */
79107 public static function enableTracking (bool $ enabled = true ): void
80108 {
0 commit comments