Coder Social home page Coder Social logo

sonatadtomodelmanager's Introduction

SonataDtoModelManager

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.