Skip to content

Commit

Permalink
Accept arrays of entities (#218)
Browse files Browse the repository at this point in the history
Co-authored-by: titiyoyo <[email protected]>
Co-authored-by: Terence <[email protected]>
  • Loading branch information
3 people authored Jul 2, 2022
1 parent 3bf17a5 commit 1ba759a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"webmozart/assert": "^1.10"
},
"require-dev": {
"doctrine/annotations": "^1.13",
"bamarni/composer-bin-plugin": "^1.4.1",
"phpspec/prophecy": "^1.14.0",
"phpspec/prophecy-phpunit": "^2.0.1",
Expand Down
5 changes: 5 additions & 0 deletions fixtures/Bridge/Symfony/Entity/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public function addUser(User $user): void
$this->users->add($user);
}
}

public function getUsers(): Collection
{
return $this->users;
}
}
21 changes: 21 additions & 0 deletions fixtures/fixture_files/user_group_array.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# This file is part of the Fidry\AliceDataFixtures package.
#
# (c) Théo FIDRY <[email protected]>
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
#

Fidry\AliceDataFixtures\Bridge\Symfony\Entity\User:
user{1..5}:
groups: '<numberBetween(2, 5)>x @group*'

Fidry\AliceDataFixtures\Bridge\Symfony\Entity\Group:
group{1..5}:
users:
- '@user1'
- '@user2'
- '@user3'
- '@user4'
- '@user5'
6 changes: 5 additions & 1 deletion src/Bridge/Doctrine/Persister/ObjectManagerPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ private function checkAssociationsMetadata(ORMClassMetadataInfo $metadata, objec
$targetEntityClassName = $associationMapping['targetEntity'];
$fieldValueOrFieldValues = $metadata->getFieldValue($object, $fieldName);

if ($fieldValueOrFieldValues instanceof Collection) {
if (is_array($fieldValueOrFieldValues)) {
foreach ($fieldValueOrFieldValues as $fieldValue) {
$this->getMetadata($targetEntityClassName, $fieldValue);
}
} elseif ($fieldValueOrFieldValues instanceof Collection) {
foreach ($fieldValueOrFieldValues->getValues() as $fieldValue) {
$this->getMetadata($targetEntityClassName, $fieldValue);
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Bridge/Symfony/Doctrine/ORMLoaderIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ public function testBidirectionalRelationships(): void
self::assertCount(5, $groups);
}

public function testArrayRelationships(): void
{
$this->loader->load([
__DIR__.'/../../../../fixtures/fixture_files/user_group_array.yml',
]);

$users = $this->doctrine->getRepository(User::class)->findAll();
$groups = $this->doctrine->getRepository(Group::class)->findAll();

self::assertCount(5, $users);
self::assertCount(5, $groups);
self::assertCount(5, $groups[0]->getUsers());
}

public function testBidirectionalRelationshipsDeclaredInDifferentFiles(): void
{
$this->loader->load([
Expand Down

0 comments on commit 1ba759a

Please sign in to comment.