xepozz/sorter-bundle

此包已被废弃,不再维护。作者建议使用xepozz/entity-sorter-bundle包。

为实体添加排序功能

安装: 4

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 1

类型:symfony-bundle

v1.0.0 2020-01-05 08:26 UTC

This package is auto-updated.

Last update: 2020-01-05 08:31:26 UTC


README

第1步:下载包

打开命令控制台,进入您的项目目录,并执行以下命令以下载此包的最新稳定版本

$ composer require xepozz/entity-sorter-bundle

此命令要求您全局安装了Composer,具体请参考Composer文档中的安装章节

第2步:将其添加到实体

将Doctrine实体监听器添加到您的实体中,并不要忘记包含所有use声明。然后根据以下示例扩展您的实体。

<?php
// AppBundle/Entity/OrderListItem.php

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="order_list_item")
 * @ORM\EntityListeners({"Xepozz\EntitySorterBundle\EventListener\SortListener"})
 */
class OrderListItem
{
    public function getId(){ /**/ }
    public function setId(){ /**/ }
    public function getSort(){ /**/ }
    public function setSort(){ /**/ }
    public function getSuperCategories(){ /**/ }
}

在此更改之后,排序值会自动为新数据库条目设置,并在您删除或更新条目时正确修改。

第3步:移动项目上下

要移动您的项目在排序顺序中,请使用实体函数moveUp($controller)moveDown($controller)。例如,您可以在控制器中调用这些函数。您的控制器类必须扩展Symfony控制器

<?php
// AppBundle/Controller/testController.php

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Xepozz\EntitySorterBundle\Utils\EntitySorter;

class TestController extends Controller
{
    private $entitySorter;

    public function __construct(EntitySorter $entitySorter) 
    {
        $this->entitySorter = $entitySorter;
    }
    
    public function moveUpAction(OrderListItem $entity)
    {
        $this->entitySorter->moveUp($entity);

        return $this->redirect('...');
    }
    
    public function moveDownAction(OrderListItem $entity)
    {
        $this->entitySorter->moveDown($entity);

        return $this->redirect('...');
    }
}

(可选)第4步:在超级分类中排序

如果您的实体是另一个实体的子分类,并且应该在它自己的超级分类中进行排序,您需要覆盖实体中的函数getSuperCategories()

在以下示例中,我们有一个需要在其产品分类内进行排序的产品子分类。

<?php
// AppBundle/Entity/ProductSubCategory.php

use Doctrine\ORM\Mapping as ORM;use Xepozz\EntitySorterBundle\Model\BaseSort;

/**
 * @ORM\Entity
 * @ORM\Table(name="product_sub_category")
 * @ORM\EntityListeners({"Xepozz\EntitySorterBundle\EventListener\SortListener"})
 */
class ProductSubCategory extends BaseSort
{
    /**
     * @ORM\ManyToOne(targetEntity="ProductCategory", inversedBy="productSubCategories")
     * @ORM\JoinColumn(name="product_category_id", referencedColumnName="id")
     */
    protected $productCategory;

    /**
     * @return array
     */
    public function getSuperCategories()
    {
        return ['productCategory' => $this->getProductCategory()];
    }
}

实体可以有几个超级分类。在getSuperCategories返回的数组中只需包含它们的值。超级分类的顺序不会影响排序。

return [
    'productCategory' => $this->getProductCategory(),
    'anotherSuperCategory' => $this->getAnotherSuperCategory()
];