14
14
use Doctrine \Persistence \ObjectRepository ;
15
15
use Symfony \Component \VarExporter \Exception \LogicException as VarExportLogicException ;
16
16
use Zenstruck \Foundry \Configuration ;
17
+ use Zenstruck \Foundry \Exception \FoundryNotBooted ;
17
18
use Zenstruck \Foundry \Exception \PersistenceDisabled ;
18
19
use Zenstruck \Foundry \Exception \PersistenceNotAvailable ;
19
20
use Zenstruck \Foundry \Factory ;
@@ -304,16 +305,17 @@ protected function normalizeParameter(string $field, mixed $value): mixed
304
305
if ($ inversedRelationshipMetadata && !$ inversedRelationshipMetadata ->isCollection ) {
305
306
$ inverseField = $ inversedRelationshipMetadata ->inverseField ;
306
307
307
- // we need to handle the circular dependency involved by inversed one-to-one relationship:
308
- // a placeholder object is used, which will be replaced by the real object, after its instantiation
309
- $ inversedObject = $ value ->withPersistMode (PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT )
310
- ->create ([$ inverseField => $ placeholder = (new \ReflectionClass (static ::class ()))->newInstanceWithoutConstructor ()]);
308
+ $ inversedObject = $ value ->withPersistMode (
309
+ $ this ->isPersisting () ? PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT : PersistMode::WITHOUT_PERSISTING
310
+ )
311
311
312
- // auto-refresh computes changeset and prevents the placeholder object to be cleanly
313
- // forgotten fom the persistence manager
314
- if ($ inversedObject instanceof Proxy) {
315
- $ inversedObject = $ inversedObject ->_real (withAutoRefresh: false );
316
- }
312
+ // we need to handle the circular dependency involved by inversed one-to-one relationship:
313
+ // a placeholder object is used, which will be replaced by the real object, after its instantiation
314
+ ->create ([
315
+ $ inverseField => $ placeholder = (new \ReflectionClass (static ::class ()))->newInstanceWithoutConstructor (),
316
+ ]);
317
+
318
+ $ inversedObject = unproxy ($ inversedObject , withAutoRefresh: false );
317
319
318
320
$ this ->tempAfterInstantiate [] = static function (object $ object ) use ($ inversedObject , $ inverseField , $ pm , $ placeholder ) {
319
321
$ pm ->forget ($ placeholder );
@@ -324,12 +326,12 @@ protected function normalizeParameter(string $field, mixed $value): mixed
324
326
}
325
327
}
326
328
327
- return unproxy (parent ::normalizeParameter ($ field , $ value ));
329
+ return unproxy (parent ::normalizeParameter ($ field , $ value ), withAutoRefresh: false );
328
330
}
329
331
330
332
protected function normalizeCollection (string $ field , FactoryCollection $ collection ): array
331
333
{
332
- if (!$ this -> isPersisting () || !$ collection ->factory instanceof self) {
334
+ if (!Configuration:: instance ()-> isPersistenceAvailable () || !$ collection ->factory instanceof self) {
333
335
return parent ::normalizeCollection ($ field , $ collection );
334
336
}
335
337
@@ -338,12 +340,15 @@ protected function normalizeCollection(string $field, FactoryCollection $collect
338
340
$ inverseRelationshipMetadata = $ pm ->inverseRelationshipMetadata (static ::class (), $ collection ->factory ::class (), $ field );
339
341
340
342
if ($ inverseRelationshipMetadata && $ inverseRelationshipMetadata ->isCollection ) {
341
- $ this ->tempAfterInstantiate [] = static function (object $ object ) use ($ collection , $ inverseRelationshipMetadata , $ field ) {
343
+ $ this ->tempAfterInstantiate [] = function (object $ object ) use ($ collection , $ inverseRelationshipMetadata , $ field ) {
342
344
$ inverseField = $ inverseRelationshipMetadata ->inverseField ;
343
345
344
- $ inverseObjects = $ collection ->withPersistMode (PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT )->create ([$ inverseField => $ object ]);
346
+ $ inverseObjects = $ collection ->withPersistMode (
347
+ $ this ->isPersisting () ? PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT : PersistMode::WITHOUT_PERSISTING
348
+ )
349
+ ->create ([$ inverseField => $ object ]);
345
350
346
- $ inverseObjects = unproxy ($ inverseObjects );
351
+ $ inverseObjects = unproxy ($ inverseObjects, withAutoRefresh: false );
347
352
348
353
// if the collection is indexed by a field, index the array
349
354
if ($ inverseRelationshipMetadata ->collectionIndexedBy ) {
@@ -379,9 +384,7 @@ protected function normalizeObject(object $object): object
379
384
return $ object ;
380
385
}
381
386
382
- if ($ object instanceof Proxy) {
383
- $ object = $ object ->_real (withAutoRefresh: false );
384
- }
387
+ $ object = unproxy ($ object , withAutoRefresh: false );
385
388
386
389
$ persistenceManager = $ configuration ->persistence ();
387
390
if (!$ persistenceManager ->hasPersistenceFor ($ object )) {
0 commit comments