Skip to content

Commit

Permalink
fixed inheritance
Browse files Browse the repository at this point in the history
matej21 committed Oct 6, 2015
1 parent 4fa576e commit f5aacdb
Showing 4 changed files with 75 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/SortableListener.php
Original file line number Diff line number Diff line change
@@ -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);
}
29 changes: 27 additions & 2 deletions tests/src/Model/Category.php
Original file line number Diff line number Diff line change
@@ -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;
}

}

/**
* @return array
*/
public function getSortableScope()
{
return $this->sortableScope;
}


/**
* @param array
*/
public function setSortableScope($sortableScope)
{
$this->sortableScope = $sortableScope;
}


}
21 changes: 21 additions & 0 deletions tests/src/Model/DescribedCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace LibretteTests\Doctrine\Sortable\Model;

use Doctrine\ORM\Mapping as ORM;
use Kdyby\Doctrine\Entities\Attributes\Identifier;
use Kdyby\Doctrine\Entities\BaseEntity;

/**
* @ORM\Entity
*/
class DescribedCategory extends Category
{
use Identifier;

/**
* @var string
* @ORM\Column(type="text")
*/
protected $description;

}
24 changes: 24 additions & 0 deletions tests/src/SortableTestCase.phpt
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ namespace LibretteTests\Doctrine\Sortable;

use Kdyby\Doctrine\EntityManager;
use LibretteTests\Doctrine\Sortable\Model\Category;
use LibretteTests\Doctrine\Sortable\Model\DescribedCategory;
use Tester\Assert;
use Tester\TestCase;

@@ -138,6 +139,29 @@ class SortableTestCase extends TestCase
}


public function testInheritance()
{
$this->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[]
*/

0 comments on commit f5aacdb

Please sign in to comment.