From f5aacdba2b19a6f7909f863f0591efdef4d3bbc2 Mon Sep 17 00:00:00 2001 From: David Matejka Date: Tue, 6 Oct 2015 11:26:48 +0200 Subject: [PATCH] fixed inheritance --- src/SortableListener.php | 4 +++- tests/src/Model/Category.php | 29 +++++++++++++++++++++++++-- tests/src/Model/DescribedCategory.php | 21 +++++++++++++++++++ tests/src/SortableTestCase.phpt | 24 ++++++++++++++++++++++ 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 tests/src/Model/DescribedCategory.php diff --git a/src/SortableListener.php b/src/SortableListener.php index 9710836..8f55065 100644 --- a/src/SortableListener.php +++ b/src/SortableListener.php @@ -156,7 +156,9 @@ public function preRemove(LifecycleEventArgs $args) */ private function createBaseQb(EntityManager $em, ISortable $sortable) { - $qb = $em->getRepository(get_class($sortable))->createQueryBuilder('e'); + $meta = $em->getClassMetadata(get_class($sortable)); + + $qb = $em->getRepository($meta->rootEntityName)->createQueryBuilder('e'); if ($sortable instanceof ISortableScope) { $this->addScope($em, $sortable, $qb); } diff --git a/tests/src/Model/Category.php b/tests/src/Model/Category.php index 0235ea0..65e864e 100644 --- a/tests/src/Model/Category.php +++ b/tests/src/Model/Category.php @@ -5,12 +5,16 @@ use Kdyby\Doctrine\Entities\Attributes\Identifier; use Kdyby\Doctrine\Entities\BaseEntity; use Librette\Doctrine\Sortable\ISortable; +use Librette\Doctrine\Sortable\ISortableScope; use Librette\Doctrine\Sortable\TSortable; /** * @ORM\Entity + * @ORM\InheritanceType(value="SINGLE_TABLE") + * @ORM\DiscriminatorMap({"category": "Category", "described": "DescribedCategory"}) + * @ORM\DiscriminatorColumn(name="type") */ -class Category extends BaseEntity implements ISortable +class Category extends BaseEntity implements ISortable, ISortableScope { use Identifier; @@ -23,10 +27,31 @@ class Category extends BaseEntity implements ISortable */ protected $name; + protected $sortableScope = []; + public function __construct($name) { $this->name = $name; } -} \ No newline at end of file + + /** + * @return array + */ + public function getSortableScope() + { + return $this->sortableScope; + } + + + /** + * @param array + */ + public function setSortableScope($sortableScope) + { + $this->sortableScope = $sortableScope; + } + + +} diff --git a/tests/src/Model/DescribedCategory.php b/tests/src/Model/DescribedCategory.php new file mode 100644 index 0000000..3060843 --- /dev/null +++ b/tests/src/Model/DescribedCategory.php @@ -0,0 +1,21 @@ +createCategories(); + $described = new DescribedCategory('Child'); + $this->em->persist($described); + $this->em->flush(); + $this->refresh([$described]); + Assert::same(7, $described->getPosition()); + } + + + public function testInheritanceWithScope() + { + $this->createCategories(); + $described = new DescribedCategory('Child'); + $described->setSortableScope(['type']); + $this->em->persist($described); + $this->em->flush(); + $this->refresh([$described]); + Assert::same(1, $described->getPosition()); + } + + /** * @return Category[] */