Optimized entity mapping for collections

This commit is contained in:
Kevin Frantz 2018-11-02 21:00:24 +01:00
parent b11b53884b
commit e0edc8935d
6 changed files with 33 additions and 24 deletions

View File

@ -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;
}
/**

View File

@ -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();
}
}

View File

@ -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;

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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());
}
}