jarjobs / sonatadtomodelmanager
用于处理使用数据传输对象(DTO)而不是实体来管理Sonata Admin表单数据的包
这个包的官方仓库似乎已不存在,因此该包已被冻结。
1.1.1
2020-10-12 09:14 UTC
Requires
- php: >=7.1
- sonata-project/admin-bundle: ^3.48 | ^4.0
- sonata-project/doctrine-orm-admin-bundle: ^3.9 | ^4.0
Conflicts
- doctrine/persistence: <1.3.4
This package is auto-updated.
Last update: 2021-04-12 10:35:15 UTC
README
安装和用法
安装
要安装SonataDtoModelManager,请运行
composer require "jarjobs/sonatadtomodelmanager:^1.0"
用法
-
在sonata.admin服务定义中的
calls
部分添加记录,并使用setModelManager
。setModelManager
的参数应该是从这个包继承的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']]
-
创建一个继承自
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 } }
-
创建一个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而不是实体。好处?实体可以始终保持正确状态,无需可空获取器方法。