@@ -302,7 +302,7 @@ protected function normalizeParameter(string $field, mixed $value): mixed
302
302
$ inversedRelationshipMetadata = $ pm ->inverseRelationshipMetadata (static ::class (), $ value ::class (), $ field );
303
303
304
304
// handle inversed OneToOne
305
- if ($ inversedRelationshipMetadata && ! $ inversedRelationshipMetadata -> isCollection ) {
305
+ if ($ inversedRelationshipMetadata?->isInverseOneToOne() ) {
306
306
$ inverseField = $ inversedRelationshipMetadata ->inverseField ;
307
307
308
308
// we need to handle the circular dependency involved by inversed one-to-one relationship:
@@ -340,7 +340,7 @@ protected function normalizeCollection(string $field, FactoryCollection $collect
340
340
341
341
$ inverseRelationshipMetadata = $ pm ->inverseRelationshipMetadata (static ::class (), $ collection ->factory ::class (), $ field );
342
342
343
- if ($ inverseRelationshipMetadata && $ inverseRelationshipMetadata ->isCollection ) {
343
+ if ($ inverseRelationshipMetadata? ->isCollection) {
344
344
$ this ->tempAfterInstantiate [] = function (object $ object ) use ($ collection , $ inverseRelationshipMetadata , $ field ) {
345
345
$ inverseField = $ inverseRelationshipMetadata ->inverseField ;
346
346
@@ -374,24 +374,37 @@ protected function normalizeCollection(string $field, FactoryCollection $collect
374
374
*
375
375
* @internal
376
376
*/
377
- protected function normalizeObject (object $ object ): object
377
+ protected function normalizeObject (string $ field , object $ object ): object
378
378
{
379
379
$ configuration = Configuration::instance ();
380
380
381
- if (
382
- !$ this ->isPersisting ()
383
- || !$ configuration ->isPersistenceAvailable ()
384
- ) {
381
+ if (!$ configuration ->isPersistenceAvailable ()) {
385
382
return $ object ;
386
383
}
387
384
388
385
$ object = unproxy ($ object , withAutoRefresh: false );
389
386
390
387
$ persistenceManager = $ configuration ->persistence ();
388
+
391
389
if (!$ persistenceManager ->hasPersistenceFor ($ object )) {
392
390
return $ object ;
393
391
}
394
392
393
+ $ inverseRelationship = $ persistenceManager ->inverseRelationshipMetadata (static ::class (), $ object ::class, $ field );
394
+
395
+ if ($ inverseRelationship && !$ inverseRelationship ->isCollection ) {
396
+ $ this ->tempAfterInstantiate [] = static function (object $ newObject ) use ($ object , $ inverseRelationship ) {
397
+ set ($ object , $ inverseRelationship ->inverseField , $ newObject );
398
+ };
399
+ }
400
+
401
+
402
+ if (
403
+ !$ this ->isPersisting ()
404
+ ) {
405
+ return $ object ;
406
+ }
407
+
395
408
if (!$ persistenceManager ->isPersisted ($ object )) {
396
409
$ persistenceManager ->scheduleForInsert ($ object );
397
410
@@ -439,11 +452,11 @@ static function(object $object, array $parameters, PersistentObjectFactory $fact
439
452
Configuration::instance ()->persistence ()->scheduleForInsert ($ object , $ afterPersistCallbacks );
440
453
}
441
454
)
442
- // ->afterPersist(
443
- // static function(object $object): void {
444
- // Configuration::instance()->persistence()->refresh($object);
445
- // }
446
- // )
455
+ ->afterPersist (
456
+ static function (object $ object ): void {
457
+ Configuration::instance ()->persistence ()->refresh ($ object );
458
+ }
459
+ )
447
460
;
448
461
}
449
462
0 commit comments