kodify/simple-crud-bundle

dev-master 2013-01-18 00:00 UTC

README

为Symfony2框架提供的简单CRUD组件。

这个组件是什么?

该组件旨在填补SonataAdminBundle与从头开始之间的差距。

功能

  • 网格:实体列表
    • 排序
    • 筛选
    • 分页
    • 可自定义单元格视图
    • 与相关实体连接
  • 从给定表单添加/编辑操作

待办事项

  • 自动添加/编辑表单生成
  • 导出
  • 国际化
  • 移除 $this->indexKey 属性并从Doctrine实体获取

安装

Composer

将以下依赖添加到您的项目 composer.json 文件中

  "require": {
      "kodify/simplecrudbundle": "dev-master"
  }

deps文件

[SimpleCrudBundle]
    git=https://github.com/kodify/SimpleCrudBundle.git
    version=origin/master

AppKernel.php

将SimpleCrudBundle添加到您应用程序的kernel中

public function registerBundles()
{
    $bundles = array(
        ...
        new Kodify\SimpleCrudBundle\KodifySimpleCrudBundle(),
        ...
    );
    ...
}

示例

控制器

/**
 * Example class.
 */

namespace Kodify\AcmeBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

use Kodify\SimpleCrudBundle\Controller\AbstractCrudController;
use Kodify\SimpleCrudBundle\Controller\CrudControllerInterface;

class CommentController extends AbstractCrudController implements CrudControllerInterface
{
    protected $pageTitle = 'Entity manager';
    protected $controllerName = 'comment';
    protected $entityClass = 'Kodify\AcmeBundle\Entity\Comment';
    protected $formClassName = 'Kodify\AcmeBundle\Form\CommentType';

    /**
     * @Route("/comment/list", name="get_comment")
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function getAction(Request $request)
    {
        $this->indexKey = 'id';//Entity id field
        //$this->addAction = false; //to show or not add input
        $this->actions = array(
            array(
                'route_name' => 'reply',//route_name + $this->controllerName should match an existing route
                'ico' => 'wrench'//http://twitter.github.com/bootstrap/base-css.html#icons
            )
        );

        return $this->renderTable();
    }

    /**
     * @Route("/comment/add", name="add_comment")
     * @Route("/comment/edit", name="edit_comment")
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function addAction(Request $request)
    {
        return parent::addAction($request);
    }

    /**
     * @Route("/comment/reply/{id}", name="reply_comment", requirements={"id"="\d+"})
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function replyCommentAction($id)
    {
        ...
    }

    /**
     * defines grid columns
     */
    public function defineTableHeader()
    {
        $tableHeader = array(
            array(
                'label' => 'Id',
                'sortable' => true,
                'filterable' => true,
                'default_sort_order' => 'DESC',
                'key' => 'id', //entity field
                'class' => 'input-micro'//input width from bootstrap [input-micro, input-mini, input-small...]
            ),
            array(
                'label' => 'Original filename',
                'sortable' => true,
                'filterable' => true,
                'key' => 'originalName',
                'filter_operator' => 'RIGHT_LIKE', // LEFT_LIKE '%term', FULL_LIKE '%term%', RIGHT_LIKE 'term%', IN, NOT IN, =, !=, >=... by default =
                'custom_cell_renderer' => 'AcmeBundle:Comment/Crud:row_original_filename_renderer.html.twig' //Custom cell renderer
                'filter_add_filter' => 'status' // key of another field to search also in the other field
            ),
            array(
                'label' => 'Status',
                'sortable' => true,
                'filterable' => true,
                'key' => 'status',
                'type' => 'options'//Select input
                'options' => Comment::getPossibleStatus(),//options for select
            ),
            array(
                'label' => 'Relationed Entity Id',
                'sortable' => true,
                'filterable' => true,
                'key' => 'Post.id',
                'class' => 'input-mini'
            ),
            array(
                'label' => 'Blocked by',
                'sortable' => false,
                'filterable' => false,
                'key' => 'blockedBy'
            )
        );

        return $tableHeader;
    }

    /**
     * default sort for grid
     */
    public function getDefaultSort()
    {
        return array('id' => 'ASC');
    }
}

存储库

简单存储库

/**
 * Example class
 */

namespace Kodify\AcmeBundle\Repository;

use Kodify\SimpleCrudBundle\Repository\AbstractCrudRepository;

class TagRepository extends AbstractCrudRepository
{

}

显示关联实体的网格的存储库

/**
 * Example class
 */

namespace Kodify\AcmeBundle\Repository;

use Kodify\SimpleCrudBundle\Repository\AbstractCrudRepository;

class ClipRepository extends AbstractCrudRepository
{
    protected $selectEntities = 'p, Post';
    protected $selectLeftJoin = array(array('field' => 'p.test', 'alias' => 'Comment'));
}

截图

Alt text