jarjobs/sonatadtomodelmanager

用于处理使用数据传输对象(DTO)而不是实体来管理Sonata Admin表单数据的包

这个包的官方仓库似乎已不存在,因此该包已被冻结。

安装次数: 3,838

依赖者: 0

建议者: 0

安全性: 0

星标: 5

关注者: 1

分支: 8

公开问题: 2

类型:symfony-bundle

1.1.1 2020-10-12 09:14 UTC

This package is auto-updated.

Last update: 2021-04-12 10:35:15 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

安装和用法

安装

要安装SonataDtoModelManager,请运行

composer require "jarjobs/sonatadtomodelmanager:^1.0"

用法

  1. 在sonata.admin服务定义中的calls部分添加记录,并使用setModelManagersetModelManager的参数应该是从这个包继承的AbstractDtoModelManager

    示例

      admin.example.entity:
        class: App\Example\Admin\ExampleAdmin
        arguments: [~, App\Example\Entity\Entity, ~]
        tags:
        - name: sonata.admin
          manager_type: orm
          label: "example label"
        calls:
          - [setModelManager, ['@App\Admin\Example\Model\ExampleModelManager']]
  2. 创建一个继承自JarJobs\SonataDtoModelManager\Model\AbstractDtoModelManager的类

    示例

    <?php
    declare(strict_types=1);
    
    namespace App\Admin\Example\Model;
    
    use JarJobs\SonataDtoModelManager\Model\AbstractDtoModelManager;
    use App\Example\Entity\Entity;
    use App\Admin\Example\Dto\ExampleDto;
    
    final class ExampleModelManager extends AbstractDtoModelManager
    {    
        protected function getSubjectClass(): string
        {
            return Entity::class;
        }
    
        protected function doCreate($dto)
        {
            // Here: create Entity based on submitted data from form as dto
        }
    
        protected function doUpdate($dto, $entity)
        {
            // Here: update $entity with validated data form in $dto
            // $entity is here for doctrine reference
        }
    
        protected function doGetModelInstance($class)
        {
            // Here: return clear dto for form
            return new ExampleDto();
        }
    
        protected function buildDto($entity)
        {
            // Here: for load form with data to update action, fill dto based on entity data 
        }
    }
  3. 创建一个DTO类,其中包含您想在实体中更新的表单字段

    示例

    <?php
       declare(strict_types=1);
        
       namespace App\Admin\Example\Dto;
    
       final class ExampleDto
       {    
           private $name;
           private $city;
        
           public function getName()
           {
               return $this->name;
           }
        
           // ...
       }

就这样!毕竟,您的表单是基于DTO而不是实体。好处?实体可以始终保持正确状态,无需可空获取器方法。