@@ -42,9 +42,6 @@ abstract class PersistentObjectFactory extends ObjectFactory
42
42
/** @var list<callable(T):void> */
43
43
private array $ tempAfterInstantiate = [];
44
44
45
- /** @var list<callable(T):void> */
46
- private array $ tempAfterPersist = [];
47
-
48
45
/**
49
46
* @phpstan-param mixed|Parameters $criteriaOrId
50
47
*
@@ -207,7 +204,7 @@ public function create(callable|array $attributes = []): object
207
204
208
205
$ this ->throwIfCannotCreateObject ();
209
206
210
- if (! $ this ->isPersisting () ) {
207
+ if ($ this ->persistMode () !== PersistMode:: PERSIST ) {
211
208
return $ object ;
212
209
}
213
210
@@ -219,12 +216,6 @@ public function create(callable|array $attributes = []): object
219
216
220
217
$ configuration ->persistence ()->save ($ object );
221
218
222
- foreach ($ this ->tempAfterPersist as $ callback ) {
223
- $ callback ($ object );
224
- }
225
-
226
- $ this ->tempAfterPersist = [];
227
-
228
219
if ($ this ->afterPersist ) {
229
220
$ attributes = $ this ->normalizedParameters ?? throw new \LogicException ('Factory::$normalizedParameters has not been initialized. ' );
230
221
@@ -254,6 +245,17 @@ final public function withoutPersisting(): static
254
245
return $ clone ;
255
246
}
256
247
248
+ /**
249
+ * @internal
250
+ */
251
+ public function withPersistMode (PersistMode $ persistMode ): static
252
+ {
253
+ $ clone = clone $ this ;
254
+ $ clone ->persist = $ persistMode ;
255
+
256
+ return $ clone ;
257
+ }
258
+
257
259
/**
258
260
* @phpstan-param callable(T, Parameters, static):void $callback
259
261
*/
@@ -272,11 +274,7 @@ protected function normalizeParameter(string $field, mixed $value): mixed
272
274
}
273
275
274
276
if ($ value instanceof self && isset ($ this ->persist )) {
275
- $ value = match ($ this ->persist ) {
276
- PersistMode::PERSIST => $ value ->andPersist (),
277
- PersistMode::WITHOUT_PERSISTING => $ value ->withoutPersisting (),
278
- PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT => $ value ->withoutPersistingButScheduleForInsert (),
279
- };
277
+ $ value = $ value ->withPersistMode ($ this ->persist );
280
278
}
281
279
282
280
if ($ value instanceof self) {
@@ -290,7 +288,7 @@ protected function normalizeParameter(string $field, mixed $value): mixed
290
288
291
289
// we need to handle the circular dependency involved by inversed one-to-one relationship:
292
290
// a placeholder object is used, which will be replaced by the real object, after its instantiation
293
- $ inversedObject = $ value ->withoutPersistingButScheduleForInsert ( )
291
+ $ inversedObject = $ value ->withPersistMode (PersistMode:: NO_PERSIST_BUT_SCHEDULE_FOR_INSERT )
294
292
->create ([$ inverseField => $ placeholder = (new \ReflectionClass (static ::class ()))->newInstanceWithoutConstructor ()]);
295
293
296
294
// auto-refresh computes changeset and prevents the placeholder object to be cleanly
@@ -325,9 +323,9 @@ protected function normalizeCollection(string $field, FactoryCollection $collect
325
323
if ($ inverseRelationshipMetadata && $ inverseRelationshipMetadata ->isCollection ) {
326
324
$ inverseField = $ inverseRelationshipMetadata ->inverseField ;
327
325
328
- $ this ->tempAfterPersist [] = static function (object $ object ) use ($ collection , $ inverseField , $ pm ) {
329
- $ collection ->create ([$ inverseField => $ object ]);
330
- $ pm -> refresh ($ object );
326
+ $ this ->tempAfterInstantiate [] = static function (object $ object ) use ($ collection , $ inverseField , $ field ) {
327
+ $ inverseObjects = $ collection-> withPersistMode (PersistMode:: NO_PERSIST_BUT_SCHEDULE_FOR_INSERT ) ->create ([$ inverseField => $ object ]);
328
+ set ($ object, $ field , unproxy ( $ inverseObjects ) );
331
329
};
332
330
333
331
// creation delegated to afterPersist hook - return empty array here
@@ -374,19 +372,32 @@ final protected function isPersisting(): bool
374
372
return false ;
375
373
}
376
374
377
- $ persistMode = $ this ->persist ?? ($ config ->persistence ()->autoPersist (static ::class ()) ? PersistMode::PERSIST : PersistMode::WITHOUT_PERSISTING );
375
+ return $ this ->persistMode ()->isPersisting ();
376
+ }
378
377
379
- return $ persistMode ->isPersisting ();
378
+ /**
379
+ * @internal
380
+ */
381
+ public function persistMode (): PersistMode
382
+ {
383
+ $ config = Configuration::instance ();
384
+
385
+ if (!$ config ->isPersistenceEnabled ()) {
386
+ return PersistMode::WITHOUT_PERSISTING ;
387
+ }
388
+
389
+ return $ this ->persist ?? ($ config ->persistence ()->autoPersist (static ::class ()) ? PersistMode::PERSIST : PersistMode::WITHOUT_PERSISTING );
380
390
}
381
391
382
392
/**
383
393
* Schedule any new object for insert right after instantiation.
394
+ * @internal
384
395
*/
385
396
final protected function initializeInternal (): static
386
397
{
387
398
return $ this ->afterInstantiate (
388
399
static function (object $ object , array $ parameters , PersistentObjectFactory $ factory ): void {
389
- if (!$ factory ->isPersisting () && (! isset ( $ factory -> persist ) || PersistMode:: NO_PERSIST_BUT_SCHEDULE_FOR_INSERT !== $ factory -> persist ) ) {
400
+ if (!$ factory ->isPersisting ()) {
390
401
return ;
391
402
}
392
403
@@ -395,14 +406,6 @@ static function(object $object, array $parameters, PersistentObjectFactory $fact
395
406
);
396
407
}
397
408
398
- private function withoutPersistingButScheduleForInsert (): static
399
- {
400
- $ clone = clone $ this ;
401
- $ clone ->persist = PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT ;
402
-
403
- return $ clone ;
404
- }
405
-
406
409
private function throwIfCannotCreateObject (): void
407
410
{
408
411
$ configuration = Configuration::instance ();
0 commit comments