wiechert/datatables-bundle

用于轻松从Doctrine实体生成datatables的扩展。

安装: 15

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 1

开放问题: 0

语言:JavaScript

类型:symfony-bundle

dev-master 2013-09-02 09:37 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:59:59 UTC


README

此Symfony 2 Bundle可以从Doctrine实体生成一个DataTable(jQuery插件),并处理服务器端进度。 WiechertDataTablesBundle还包含一些有趣的特性,这些特性在示例部分有详细说明。

#####为订单位置(及其相关实体)生成的DataTable:

目录

  • 核心功能
  • 安装
  • 示例
  • 自定义视图
  • 配置参考

核心功能

  • 让Bundle为给定的Doctrine实体生成DataTable,以及
  • 服务器端搜索、排序、JSON序列化
  • 可定制的视图,使用排除策略
  • EntityDisplayer为您的实体创建可读的视图,并使用NamedDatatables显示额外信息

安装

###1. 通过composer安装

将bundle添加到composer.json中的依赖项。

  "require": {
		...
		"wiechert/datatables-bundle": "dev-master",
		...
    },

在shell中运行以下命令以安装WiechertDataTablesBundle及其依赖项

php composer.phar update wiechert/datatables-bundle

###2. 允许路由

您必须允许bundle的路由配置。将以下代码添加到您的routing.yml

wiechert_data_tables:
	resource: "@WiechertDataTablesBundle/Controller/"
    type:     annotation
    prefix:   /

fos_js_routing:
    resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"

您可以自定义前缀。

###3. 允许Twig资产管理

该bundle使用Twig的资产管理,必须在您的config.yml中允许

assetic:
	...
    bundles:        [ WiechertDataTablesBundle ]

###4. 更新您的AppKernel.php

确保您的AppKernel.php注册了以下bundle

new JMS\SerializerBundle\JMSSerializerBundle(),
new FOS\JsRoutingBundle\FOSJsRoutingBundle(),
new Wiechert\DataTablesBundle\WiechertDataTablesBundle(),

###4. 配置

请参阅示例部分,创建适用于您的用例的配置文件。

示例

###1. 注释您的实体类

可能有一个指向类 User(它是其创建者)的实体类 Category。我们使用默认的Doctrine注释,并添加了称为 DisplayNameGroups 的注释。

#####注释类别类

<?php
  
namespace Wiechert\DataTablesBundle\Entity;

      use Doctrine\ORM\Mapping as ORM;
      use JMS\Serializer\Annotation as Serializer;
      use Wiechert\DataTablesBundle\Annotations\DisplayName;


      /**
       * @ORM\Entity
       * @ORM\Table(name="category")
       * @DisplayName(name="Category")
       */
      class Category
      {
      	 /**
      	  * @ORM\Id
      	  * @ORM\Column(type="integer", name="id")
      	  * @ORM\GeneratedValue(strategy="AUTO")
      	  * @Serializer\Groups({"Id"})
      	  * @DisplayName(name="Id")
      	  */
      	 protected $id;
      	 

         /**
      	* @ORM\Column(type="string", length=60)
      	* @Serializer\Groups({"Simple", "Name"})
      	* @DisplayName(name="Label")
      	*/
      	protected $label;



      	/**
      	 * @ORM\OneToMany(targetEntity="Category", mappedBy="rootcategory")
      	 * @Serializer\Groups({"ComplexeReference"})
      	**/
      	protected $subcategories;



      	/**
      	 * @ORM\ManyToOne(targetEntity="Wiechert\DataTablesBundle\Entity\User", inversedBy="categories")
      	 * @Serializer\Groups({"SimpleReference"})
      	 * @DisplayName(name="Creator")
      	 **/
      	protected $creator;

      	/**
      	 * @ORM\ManyToOne(targetEntity="Wiechert\DataTablesBundle\Entity\Category", inversedBy="subcategories")
      	 * @Serializer\Groups({"SimpleReference"})
      	 * @DisplayName(name="Root Category")
      	 **/
      	protected $rootcategory;

      	/**
      	 * @ORM\ManyToMany(targetEntity="Product", mappedBy="categories")
      	 * @Serializer\Groups({"ComplexeReference"})
      	 * @DisplayName(name="Products")
      	 */
      	protected $products;
      	
      	.....
      	}

#####注释用户类

	
<?php

namespace Wiechert\DataTablesBundle\Entity;

	use Doctrine\ORM\Mapping as ORM;
	use JMS\Serializer\Annotation as Serializer;
	use Wiechert\DataTablesBundle\Annotations\DisplayName;


	/**
	 * @ORM\Entity
	 * @ORM\Table(name="user")
	 * @DisplayName(name="User")
	 */
	class User
	{
		 /**
		  * @ORM\Id
		  * @ORM\Column(type="integer", name="id")
		  * @ORM\GeneratedValue(strategy="AUTO")
		  * @Serializer\Groups({"Id"})
		  * @DisplayName(name="Id")
		  */
		 protected $id;
		 

         /**
          * @ORM\Column(type="string", length=60)
          * @Serializer\Groups({"Simple", "Name"})
          * @DisplayName(name="Username")
          */
         protected $username;

         /**
          * @ORM\OneToMany(targetEntity="Category", mappedBy="creator")
          * @Serializer\Groups({"ComplexeReference"})
          **/
         protected $categories;


         /**
          * @ORM\OneToMany(targetEntity="Order", mappedBy="user")
          * @Serializer\Groups({"ComplexeReference"})
          **/
         protected $orders;

		...
		}

DisplayName 注释允许您指定渲染列的标签,这是可选的。默认情况下,bundle将使用名称属性。

要自定义显示哪些属性和实体,您可以使用排除策略(稍后将进一步说明)。我们的方法是将属性分配给组使用 Groups 注释。然后我们使用一个排除策略,它将根据属性组来决定是否反映它。

###2. 创建配置文件

bundle需要一个配置文件,该文件定义了哪些实体可以显示,在哪里可以找到它们,可以应用哪些操作等(参见配置参考)。创建一个空配置文件,并在您的app/config.yml中引用它

imports:
   - { resource: "@Bundle/Resources/config/yourfile.yml" }

给定场景的配置文件可能如下所示

wiechert_data_tables:
    Datatables:
        Bundles:
          WiechertDataTablesBundle:
            namespace: "Wiechert\\DataTablesBundle\\Entity\\"
            Tables:
              Category:
                  display_name:         "All the categories"
                  title:                "Category overview"
                  Actions:
                    PHPActions:
                      - {name: "Display", route: "wiechert_core_generic_entity_display"}
                  NamedTables:
                      get_subcategories:
                          title:               "sub categories"
                          description:         "get all sub categories"
                          select_table:         "Category"
                          select_table_bundle:  "WiechertDataTablesBundle"
                          joins:
                              - {join: "e0.rootcategory", alias: "a"}
                          where_caluse:         "a.id = :id"

                      get_rootcategorie:
                            title:               "root categorie"
                            description:         "get  root categorie"
                            select_table:         "Category"
                            select_table_bundle:  "WiechertDataTablesBundle"
                            joins:
                                - {join: "e0.subcategories", alias: "a"}
                            where_caluse:         "a.id = :id"

                      get_products:
                           title:              "every product of this category"
                           description:        "list all producs of this category"
                           select_table:         "Product"
                           select_table_bundle:  "WiechertDataTablesBundle"
                           joins:
                              - {join: "e0.categories", alias: "a"}
                           where_caluse:         "a.id = :id"

              Product:
                  display_name:         "All the products"
                  title:                "Product overview"
                  Actions:
                      PHPActions:
                         - {name: "Display", route: "wiechert_core_generic_entity_display"}
                  NamedTables:
                     get_categories:
                                      title:               "Categories of the product"
                                      description:         "get all categories"
                                      select_table:         "Category"
                                      select_table_bundle:  "WiechertDataTablesBundle"
                                      joins:
                                           - {"e0.products", alias: "a"}
                                      where_caluse:         "a.id = :id"

                     get_buyers:
                           title:               "Buyery of this product"
                           description:         "get all buyers"
                           select_table:         "User"
                           select_table_bundle:  "WiechertDataTablesBundle"
                           joins:
                                - {join: "e0.orders", alias: "a"}
                                - {join: "a.positions", alias: "b"}
                                - {join: "b.product", alias: "c"}
                           where_caluse:         "c.id = :id"

              User:
                  display_name:         "All the users"
                  title:                "User overview"
                  Actions:
                      PHPActions:
                         - {name: "Display", route: "wiechert_core_generic_entity_display"}


####2.1 定义PHP-动作

大部分内容都是自我解释的。让我来解释一下PHPActions数组。PHPAction可以是指定的任何类型的操作(在Symfony控制器中),比如一个名为deleteEntityByIdAction的操作可以删除一个实体。一个适用于类别的操作可以是createSubcategoryAction,它以表单的形式显示,允许你添加子类别。

操作可以应用于DataTable中的任何实体。每个实体的所有可用操作以下拉列表的形式在DataTable中呈现。

当捆绑包调用该操作时,它会传递实体的名称、捆绑包名称、标识符以及使用的排除策略名称。你的控制器可能不需要所有这些信息,因此不强制实现该接口。

#####示例:一个删除操作

 ...
    public function deleteEntityAction($bundle, $entity, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $query = $em->createQueryBuilder()
            ->select("e")
            ->from($bundle.$entity, "e")
            ->where("e.id = :id")
            ->getQuery()
            ->setParameter('id', $id);

        try {
            $object = $query->getSingleResult();
            $em->remove($object);
            $em->flush();
            // render success page

        } catch(\Exception $e) {
           // render error page

        }


    }
    ..

####2.2 定义命名DataTable

一个有趣的功能是命名DataTable。这些DataTable显示特定实体的额外信息。给定的配置允许捆绑包显示给定类别的所有子类别或所有相关产品(get_subcategories)。

#####其他可能的场景

  • 显示订单的所有位置
  • 显示包含特定产品的订单
  • 显示组中的所有用户...

只需配置从你实际想要显示的实体开始的内部连接链。

使用EntityDisplayer时,将列出命名DataTable。EntityDisplayer为给定实体生成可读的视图。此视图显示了实体的属性和所有相关的命名DataTable。

#####产品实体Displayer:

#####类别实体Displayer:

###3. 显示你的DataTable

路由是:http://local.test/app_dev.php/~/datatable/generate/{bundle}/{entity}/{strategy}

  • {bundle}, {entity}:如配置所示
  • {strategy}:排除策略名称(预定义:Simple或Extended,或您的实现)

(其中~已替换为您定义的后缀)

示例: http://local.test/app_dev.php/datatable/generate/WiechertDataTablesBundle/Category/Extended

EntityDisplayer可以作为操作应用,或者通过路由调用它

http://local.test/app_dev.php/~/datatable/display/{bundle}/{entity}/{strategy}/{id}

示例: http://local.test/app_dev.php/datatable/display/WiechertDataTablesBundle/Product/Simple/2

  • id:实体的标识符(必须命名为id)

自定义视图

###1. 排除策略的使用

排除策略会影响在决定是否跳过属性(或类)时的反射过程。

####1.1 基于组的排除策略

一个流行的做法是定义组。所以如果一个属性(或更进一步的成员)不属于预定义的组之一,它就会被跳过。

抽象类TreeGroupExclusionStrategy期望子类实现getGroupsgetMaxDepth方法。

  • getGroups:必须返回一个多维数组,其中第一个维度与图深度匹配,第二个维度与该图深度允许的组匹配。

参考实现ExtendedStrategy允许最大图深度为3。

namespace Wiechert\DataTablesBundle\TableGenerator\EntityReflection\Strategies;


class ExtendedStrategy extends TreeGroupExclusionStrategy
{
    /**
     * @return string[]
     */
    public  function getGroups()
    {
        return array ( array(parent::$idGruppe, parent::$simpleGruppe, parent::$simpleReferenceGruppe),
                      array(parent::$idGruppe, parent::$simpleGruppe,  parent::$simpleReferenceGruppe), array(parent::$idGruppe, parent::$simpleGruppe));
    }


    /**
     *
     * @return int
     */
    public function getMaxDepth()
    {
        return 3;
    }

    /**
     * Returns the name of the display strategy.
     *
     * @return string
     */
    public function getName()
    {
        return "ExtendedStrategy";
    }


}

####1.2 其他排除策略

您可以实现 interface Wiechert\DataTablesBundle\TableGenerator\EntityReflection\Strategies\IExclusionStrategy 接口。

配置参考