用于 Nette 框架的 Doctrine 可排序实体。

v0.3.0 2018-02-21 14:24 UTC

This package is auto-updated.

Last update: 2024-08-27 21:33:46 UTC


README

迟早你需要实现实体排序。例如分类、主页上的产品等。为什么你自己做,而只需要复制粘贴就能完成呢?

安装

安装 librette/doctrine-sortable 的最佳方式是使用 Composer

$ composer require librette/doctrine-sortable

并在你的 `config.neon` 中启用 librette 扩展

extensions:
	# add this line at the end of your extensions list
	librette.doctrine.sortable: Librette\Doctrine\Sortable\DI\SortableExtension
```

Simplest entity
---------------


```php
namespace App;

use Kdyby\Doctrine\Entities\BaseEntity;
use Librette\Doctrine\Sortable\ISortable;
use Librette\Doctrine\Sortable\TSortable;

/**
 * @ORM\Entity
 */
class Category extends BaseEntity implements ISortable
{
	use TSortable;
	/**
	 * @ORM\Id
	 * @ORM\Column(type="integer")
	 * @ORM\GeneratedValue
	 */
	protected $id;
}
```

Trait TSortable
---------------

There is trait `TSortable` that implements basic sorting methods to your entity.
Everything you need is to call those methods in your service / presenter.

```php
// you can move your entity up or down
$entity->moveUp();
$entity->moveDown();
// or you can put it before / after another one
$entity->moveBefore($anotherEntity);
$entity->moveAfter($anotherEntity);
// also you can set position directly
$entity->setPosition(21);
```

**Don't forget to persist and flush after you finish with sorting!**

```php
$this->em->persist($entity);
$this->em->flush();
```