To install SonataDtoModelManager run:
composer require "jarjobs/sonatadtomodelmanager:^1.0"
-
Add in
calls
section record withsetModelManager
in sonata.admin service definitions. ThesetModelManager
argument should be your inheritance ofAbstractDtoModelManager
from this bundle.Example:
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']]
-
Create class which inherit
JarJobs\SonataDtoModelManager\Model\AbstractDtoModelManager
Example:
<?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 } }
-
Create DTO class with form fields which you wanna update in entity
Example:
<?php declare(strict_types=1); namespace App\Admin\Example\Dto; final class ExampleDto { private $name; private $city; public function getName() { return $this->name; } // ... }
That is it! After all, your form is based on DTO instead of Entity. Benefit? Entity can be always in proper state without nullable getter methods.