From e0edc8935de3dac7768913407c22005c3efca438 Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Fri, 2 Nov 2018 21:00:24 +0100 Subject: [PATCH] Optimized entity mapping for collections --- .../src/Entity/Attribut/MembersAttribut.php | 8 ++++--- application/src/Entity/Meta/Reciever.php | 5 ++-- .../src/Entity/Source/AbstractSource.php | 2 +- .../Collection/AbstractCollectionSource.php | 23 ++++++++++++++++++- .../Collection/MemberCollectionSource.php | 15 ------------ .../AbstractCollectionSourceTest.php | 4 ++-- 6 files changed, 33 insertions(+), 24 deletions(-) diff --git a/application/src/Entity/Attribut/MembersAttribut.php b/application/src/Entity/Attribut/MembersAttribut.php index 5172ffd..17d3133 100644 --- a/application/src/Entity/Attribut/MembersAttribut.php +++ b/application/src/Entity/Attribut/MembersAttribut.php @@ -7,22 +7,24 @@ use App\Helper\DimensionHelper; /** * @author kevinfrantz + * + * @todo this attribut should be refactored to mapp fully on collections instead of members */ trait MembersAttribut { /** * @var Collection */ - protected $members; + protected $collection; public function getMembers(): Collection { - return $this->members; + return $this->collection; } public function setMembers(Collection $members): void { - $this->members = $members; + $this->collection = $members; } /** diff --git a/application/src/Entity/Meta/Reciever.php b/application/src/Entity/Meta/Reciever.php index 5b5b186..fc66b37 100644 --- a/application/src/Entity/Meta/Reciever.php +++ b/application/src/Entity/Meta/Reciever.php @@ -37,10 +37,11 @@ class Reciever extends AbstractMeta implements RecieverInterface * * @var ArrayCollection | SourceInterface[] */ - protected $members; + protected $collection; public function __construct() { - $this->members = new ArrayCollection(); + parent::__construct(); + $this->collection = new ArrayCollection(); } } diff --git a/application/src/Entity/Source/AbstractSource.php b/application/src/Entity/Source/AbstractSource.php index 0d2e4ae..c19272f 100644 --- a/application/src/Entity/Source/AbstractSource.php +++ b/application/src/Entity/Source/AbstractSource.php @@ -43,7 +43,7 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface * @todo Rename table to use the right schema * * @var Collection|MemberCollectionSource[] - * @ORM\ManyToMany(targetEntity="App\Entity\Source\Collection\MemberCollectionSource",mappedBy="members") + * @ORM\ManyToMany(targetEntity="App\Entity\Source\Collection\MemberCollectionSource",mappedBy="collection") */ protected $memberships; diff --git a/application/src/Entity/Source/Collection/AbstractCollectionSource.php b/application/src/Entity/Source/Collection/AbstractCollectionSource.php index ac377fa..335e43e 100644 --- a/application/src/Entity/Source/Collection/AbstractCollectionSource.php +++ b/application/src/Entity/Source/Collection/AbstractCollectionSource.php @@ -4,13 +4,34 @@ namespace App\Entity\Source\Collection; use App\Entity\Source\AbstractSource; use App\Entity\Attribut\CollectionAttribut; +use Doctrine\Common\Collections\Collection; +use App\Entity\Source\SourceInterface; +use Doctrine\ORM\Mapping as ORM; +use Doctrine\Common\Collections\ArrayCollection; /** - * @todo Implement inhiering classes! + * @ORM\Entity + * @ORM\Table(name="source_collection") + * @ORM\InheritanceType("JOINED") + * @ORM\DiscriminatorColumn(name="discr", type="string") + * @ORM\DiscriminatorMap({"member" = "MemberCollectionSource"}) * * @author kevinfrantz */ abstract class AbstractCollectionSource extends AbstractSource implements CollectionSourceInterface { use CollectionAttribut; + + /** + * @var Collection|SourceInterface[] + * @ORM\ManyToMany(targetEntity="App\Entity\Source\AbstractSource",inversedBy="memberships") + * @ORM\JoinTable(name="source_group_members") + */ + protected $collection; + + public function __construct() + { + parent::__construct(); + $this->collection = new ArrayCollection(); + } } diff --git a/application/src/Entity/Source/Collection/MemberCollectionSource.php b/application/src/Entity/Source/Collection/MemberCollectionSource.php index 57589c6..62dab16 100644 --- a/application/src/Entity/Source/Collection/MemberCollectionSource.php +++ b/application/src/Entity/Source/Collection/MemberCollectionSource.php @@ -3,9 +3,7 @@ namespace App\Entity\Source\Collection; use Doctrine\ORM\Mapping as ORM; -use Doctrine\Common\Collections\Collection; use App\Entity\Attribut\MembersAttribut; -use Doctrine\Common\Collections\ArrayCollection; /** * @author kevinfrantz @@ -15,17 +13,4 @@ use Doctrine\Common\Collections\ArrayCollection; final class MemberCollectionSource extends AbstractCollectionSource implements MemberCollectionSourceInterface { use MembersAttribut; - - /** - * @var Collection - * @ORM\ManyToMany(targetEntity="App\Entity\Source\AbstractSource",inversedBy="memberships") - * @ORM\JoinTable(name="source_group_members") - */ - protected $members; - - public function __construct() - { - parent::__construct(); - $this->members = new ArrayCollection(); - } } diff --git a/application/tests/Unit/Entity/Source/Collection/AbstractCollectionSourceTest.php b/application/tests/Unit/Entity/Source/Collection/AbstractCollectionSourceTest.php index 7876b98..b16484a 100644 --- a/application/tests/Unit/Entity/Source/Collection/AbstractCollectionSourceTest.php +++ b/application/tests/Unit/Entity/Source/Collection/AbstractCollectionSourceTest.php @@ -5,6 +5,7 @@ namespace Tests\Unit\Entity\Source\Collection; use PHPUnit\Framework\TestCase; use App\Entity\Source\Collection\CollectionSourceInterface; use App\Entity\Source\Collection\AbstractCollectionSource; +use Doctrine\Common\Collections\Collection; class AbstractCollectionSourceTest extends TestCase { @@ -21,7 +22,6 @@ class AbstractCollectionSourceTest extends TestCase public function testConstruct(): void { - $this->expectException(\TypeError::class); - $this->collectionSource->getCollection(); + $this->assertInstanceOf(Collection::class, $this->collectionSource->getCollection()); } }