pprolette/crud-bundle

此包为 Doctrine 实体提供基本的 CRUD 端点

安装: 2

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

v0.1.1 2024-03-01 09:36 UTC

This package is auto-updated.

Last update: 2024-09-30 10:55:26 UTC


README

此包为给定 Doctrine 实体提供基本的 CRUD 端点

  • GET /entity 返回分页结果
  • GET /entity/list 返回对象数组
  • GET /entity/{id} 返回对象
  • POST /entity 创建实体
  • PUT /entity/{id} 更新实体
  • DELETE '/entity/{id} 删除实体

安装

打开命令行控制台,进入您的项目目录并执行

$ composer require pportelette/crud-bundle

不使用 Symfony Flex 的应用程序

然后,通过将其添加到项目 config/bundles.php 文件中已注册的包列表中来启用该包

// config/bundles.php

return [
    Pportelette\CrudBundle\PporteletteCrudBundle::class => ['all' => true],
];

使用方法

在以下部分中,我们将假设我们有一个简单的 Doctrine 实体 'Category' 及其 Doctrine 仓库 'CategoryRepository'。

namespace App\Entity;

use App\Repository\CategoryRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: CategoryRepository::class)]
class Category
{ 
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: "integer")]
    private $id;

    #[ORM\Column(type: "string", length: 100)]
    private $name;

    #[ORM\Column(type: "datetime_immutable")]
    private $created_at;

    /*
        GETTERS AND SETTERS
    */
}

创建 ViewModel

首先,需要创建一个 ViewModel 对象,该对象将代表我们在前端中的实体。

namespace App\Model;

use Pportelette\CrudBundle\Model\ViewModel;
use App\Entity\Category;

class CategoryVM extends ViewModel {
    public $id;
    public $name;
    private $createdAt;

    public function fromEntity(Category $category = null): void {
        if(!$category) {
            return;
        }
        $this->id = $category->getId();
        $this->name = $category->getName();
        $this->createdAt = $category->getCreatedAt();
    }

    public function toEntity(Category $category = new Category()): Category {
        $category->setName($this->name);
        $category->setCreatedAt($this->createdAt);

        return $category;
    }

    /**
     * Customize the response for the GET /category endpoint
     * Optional
     */
    public function getAll(): array {
        return [
            'id' => $this->id,
            'name' => $this->name
        ];
    }

    /**
     * Customize the response for the GET /category/list endpoint
     * Optional
     */
    public function getList(): array {
        return [
            'name' => $this->name,
        ];
    }
}

扩展仓库

namespace App\Repository;

// ...
use Pportelette\CrudBundle\Repository\CrudRepository;

class CategoryRepository extends CrudRepository
{
    // ...
}

扩展控制器

namespace App\Controller;

// ...
use Pportelette\CrudBundle\Controller\CrudController;

class WordController extends CrudController
{
    public function __construct(SerializerInterface $serializer, CategoryRepository $repository)
    {
        $this->configure(
            $serializer,
            $repository, 
            CategoryVM::class
        );
    }
}

配置路由

将所有以 /category 开头的请求驱动到 CategoryController。

category:
    resource: ../src/Controller/CategoryController.php
    type: attribute
    prefix:   /category
    trailing_slash_on_root: false

在此阶段,6 个 CRUD 端点都是可用的。

自定义服务

可以覆盖服务方法。

为此,创建一个服务 CategoryService.php,该服务扩展 CrudService 并覆盖一个符合 Pportelette\CrudBundle\Service\CrudServiceInterface 的方法,例如

public function getAll(int $page, array $filters = []): Pageable;
public function getList(array $filters = []): array;
public function getEntity(int $id): ViewModel;
public function createEntity(array $properties): ViewModel;
public function updateEntity(int $id, array $properties): ViewModel;
public function deleteEntity(int $id): void;
// src/Service/CategoryService.php
// ...
use Pportelette\CrudBundle\Service\CrudService;
use Pportelette\PageableBundle\Model\Pageable;

class WordService extends CrudService
{
    public function __construct(CategoryRepository $wordRepository)
    {
        parent::__construct($wordRepository, CategoryVM::class);
    }

    public function getAll(int $page, array $params = []): Pageable
    {    
        // Your custom code
    }
}

自定义仓库

可以覆盖仓库方法。

您的仓库已经扩展了 CrudRepository。只需添加一个符合 Pportelette\CrudBundle\Repository\CrudRepositoryInterface 的方法即可

public function getAll(int $page, array $filters = []): Pageable;
public function getList(array $filters = []): array;
// src/Repository/CategoryRepository.php

// ...

public function getAll(int $page, array $filters = []): Pageable
{
    $qb = $this->createQueryBuilder('w');

    // Your custom code

    return $this->getPage(
        $qb,
        $page
    );
}