jarjobs/sonatadtomodelmanager

Bundle for handling Sonata Admin form data with data-transfer-object instead of entity

Installs: 3 838

Dependents: 0

Suggesters: 0

Security: 0

Stars: 5

Watchers: 1

Forks: 8

Open Issues: 2

Type: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

Installation and usage

Installation

To install SonataDtoModelManager run:

composer require "jarjobs/sonatadtomodelmanager:^1.0"

Usage

  1. Add in calls section record with setModelManager in sonata.admin service definitions. The setModelManager argument should be your inheritance of AbstractDtoModelManager 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']]
  2. 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 
        }
    }
  3. 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.