Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b0a5703

Browse files
authored
fix: call inverse relatoinship callback before afterInstantiate() (#1041)
1 parent c61be3a commit b0a5703

5 files changed

Lines changed: 37 additions & 14 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
/docker-compose.override.yaml
1717
/tests/Fixture/Maker/tmp/
1818
/.phpbench
19+
/config/reference.php

phpunit-deprecation-baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ See https://github.com/zenstruck/foundry/blob/2.x/UPGRADE-2.7.md to upgrade.]]><
1818
<issue><![CDATA[Since doctrine/mongodb-odm 2.14: Not using native lazy objects is deprecated and will be impossible in Doctrine MongoDB ODM 3.0.]]></issue>
1919
<issue><![CDATA[Since symfony/framework-bundle 7.3: Not setting the "property_info.with_constructor_extractor" option explicitly is deprecated because its default value will change in version 8.0.]]></issue>
2020
<issue><![CDATA[Since symfony/console 7.4: The "Symfony\Component\Console\Application::add()" method is deprecated and will be removed in Symfony 8.0, use "Symfony\Component\Console\Application::addCommand()" instead.]]></issue>
21+
<issue><![CDATA[Since doctrine/mongodb-odm 2.3: Document short namespace aliases are deprecated, use ::class constant instead.]]></issue>
2122
</line>
2223
</file>
2324
<file path="vendor/doctrine/deprecations/src/Deprecation.php">

src/Persistence/PersistentObjectFactory.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ abstract class PersistentObjectFactory extends ObjectFactory
5050
private array $afterPersist = [];
5151

5252
/** @var list<callable(T):void> */
53-
private array $tempAfterInstantiate = [];
53+
private array $inverseRelationshipCallbacks = [];
5454

5555
private bool $isRootFactory = true;
5656

@@ -248,12 +248,6 @@ public function create(callable|array $attributes = []): object
248248

249249
$object = parent::create($attributes);
250250

251-
foreach ($this->tempAfterInstantiate as $callback) {
252-
$callback($object);
253-
}
254-
255-
$this->tempAfterInstantiate = [];
256-
257251
$this->throwIfCannotCreateObject();
258252

259253
if (PersistMode::PERSIST !== $this->persistMode()) {
@@ -401,7 +395,7 @@ protected function normalizeParameter(string $field, mixed $value): mixed
401395
;
402396

403397
if (($fieldType = (new \ReflectionClass(static::class()))->getProperty($field)->getType())?->allowsNull()) {
404-
$this->tempAfterInstantiate[] = static function(object $object) use ($value, $inverseField, $field) {
398+
$this->inverseRelationshipCallbacks[] = static function(object $object) use ($value, $inverseField, $field) {
405399
$inverseObject = $value->create([$inverseField => $object]);
406400

407401
set($object, $field, ProxyGenerator::unwrap($inverseObject, withAutoRefresh: false));
@@ -416,7 +410,7 @@ protected function normalizeParameter(string $field, mixed $value): mixed
416410
withAutoRefresh: false
417411
);
418412

419-
$this->tempAfterInstantiate[] = static function(object $object) use ($inverseObject, $inverseField) {
413+
$this->inverseRelationshipCallbacks[] = static function(object $object) use ($inverseObject, $inverseField) {
420414
set($inverseObject, $inverseField, $object);
421415
};
422416

@@ -445,7 +439,7 @@ protected function normalizeCollection(string $field, FactoryCollection $collect
445439
$collection = $collection->notRootFactory();
446440

447441
if ($inverseRelationshipMetadata instanceof OneToManyRelationship) {
448-
$this->tempAfterInstantiate[] = function(object $object) use ($collection, $inverseRelationshipMetadata, $field) {
442+
$this->inverseRelationshipCallbacks[] = function(object $object) use ($collection, $inverseRelationshipMetadata, $field) {
449443
$inverseField = $inverseRelationshipMetadata->inverseField();
450444

451445
$inverseObjects = $collection
@@ -497,13 +491,13 @@ protected function normalizeObject(string $field, object $object): object
497491
$inverseRelationship = $persistenceManager->bidirectionalRelationshipMetadata(static::class(), $object::class, $field);
498492

499493
if ($inverseRelationship instanceof OneToOneRelationship) {
500-
$this->tempAfterInstantiate[] = static function(object $newObject) use ($object, $inverseRelationship) {
494+
$this->inverseRelationshipCallbacks[] = static function(object $newObject) use ($object, $inverseRelationship) {
501495
Hydrator::set($object, $inverseRelationship->inverseField(), $newObject, catchErrors: true);
502496
};
503497
}
504498

505499
if ($inverseRelationship instanceof ManyToOneRelationship) {
506-
$this->tempAfterInstantiate[] = static function(object $newObject) use ($object, $inverseRelationship) {
500+
$this->inverseRelationshipCallbacks[] = static function(object $newObject) use ($object, $inverseRelationship) {
507501
Hydrator::add($object, $inverseRelationship->inverseField(), $newObject);
508502
};
509503
}
@@ -559,7 +553,17 @@ static function(object $object, array $parameters, PersistentObjectFactory $fact
559553
return $factory;
560554
}
561555

562-
return $factory->afterPersist(
556+
return $factory->afterInstantiate(
557+
static function(object $object, array $parameters, self $factoryUsed): void {
558+
$tempAfterInstantiateCallbacks = $factoryUsed->inverseRelationshipCallbacks;
559+
$factoryUsed->inverseRelationshipCallbacks = [];
560+
foreach ($tempAfterInstantiateCallbacks as $tempAfterInstantiateCallback) {
561+
$tempAfterInstantiateCallback($object);
562+
}
563+
},
564+
priority: 1000
565+
)
566+
->afterPersist(
563567
static function(object $object, array $parameters, self $factoryUsed): bool {
564568
Configuration::instance()->eventDispatcher()->dispatch(
565569
new AfterPersist($object, $parameters, $factoryUsed)

src/ZenstruckFoundryBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function boot(): void
4848

4949
public function configure(DefinitionConfigurator $definition): void
5050
{
51-
$definition->rootNode() // @phpstan-ignore method.notFound
51+
$definition->rootNode() // @phpstan-ignore class.notFound
5252
->children()
5353
->booleanNode('auto_refresh_proxies')
5454
->info('Whether to auto-refresh proxies by default (https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#auto-refresh)')

tests/Integration/ORM/EntityRelationship/EntityFactoryRelationshipTestCase.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,23 @@ public function can_create_many_with_factory_from_data_provider_with_relationshi
815815
self::assertCount(2, $objects);
816816
}
817817

818+
/** @test */
819+
#[Test]
820+
#[DataProvider('provideCascadeRelationshipsCombinations')]
821+
#[IgnorePhpunitWarnings(EdgeCasesRelationshipTest::DATA_PROVIDER_WARNING_REGEX)]
822+
#[UsingRelationships(Category::class, ['contacts'])]
823+
public function it_sets_one_to_many_before_after_instantiate(): void
824+
{
825+
static::categoryFactory()
826+
->afterInstantiate(function(Category $category) {
827+
self::assertCount(3, $category->getContacts());
828+
})
829+
->create([
830+
'contacts' => static::contactFactory()->many(3),
831+
])
832+
;
833+
}
834+
818835
public static function provideCanUseFactoryInDataProviderWithRelationshipCases(): iterable
819836
{
820837
yield [

0 commit comments

Comments
 (0)