Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bundles
  4. SonataAdminBundle
  5. Using DataMapper to work with domain entities without per field setters

Using DataMapper to work with domain entities without per field setters

Edit this page

This is an example of using DataMapper with entities that avoid setters/getters for each field.

Pre-requisites

  • You already have SonataAdmin and DoctrineORM up and running.
  • You already have an Entity class, in this example that class will be called Example.
  • You already have an Admin set up, in this example it's called ExampleAdmin.

The recipe

If there is a requirement for the entity to have domain specific methods instead of getters/setters for each entity field then it won't work with SonataAdmin out of the box. But Symfony Form component provides DataMapper that can be used to make it work. Symfony itself lacks examples of using DataMapper but there is an article by webmozart that covers it - https://webmozart.io/blog/2015/09/09/value-objects-in-symfony-forms/

Example Entity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// src/Entity/Example.php

namespace App\Entity;

final class Example
{
    private string $name;

    private string $description;

    public function __construct(string $name, string $description)
    {
        $this->name = $name;
        $this->description = $description;
    }

    public function update(string $description)
    {
        $this->description = $description
    }

    // rest of the code goes here
}

DataMapper

To be able to set entity data without the possibility to use setters a DataMapper should be created:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// src/Form/DataMapper/ExampleDataMapper.php

namespace App\Form\DataMapper;

use Symfony\Component\Form\DataMapperInterface;
use App\Entity\Example;

final class ExampleDataMapper implements DataMapperInterface
{
    /**
     * @param Example $data
     * @param FormInterface[]|\Traversable $forms
     */
    public function mapDataToForms($data, $forms)
    {
        if (null !== $data) {
            $forms = iterator_to_array($forms);
            $forms['name']->setData($data->getName());
            $forms['description']->setData($data->getDescription());
        }
    }

    /**
     * @param FormInterface[]|\Traversable $forms
     * @param Example $data
     */
    public function mapFormsToData($forms, &$data)
    {
        $forms = iterator_to_array($forms);

        if (null === $data->getId()) {
            $name = $forms['name']->getData();
            $description = $forms['description']->getData();

            // New entity is created
            $data = new Example(
                $name,
                $description
            );
        } else {
            $data->update(
                $forms['description']->getData()
            );
        }
    }
}

Admin class

Now we need to configure the form to use our ExampleDataMapper:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// src/Admin/ExampleAdmin.php

namespace App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\FormMapper;
use App\Form\DataMapper\ExampleDataMapper;

final class ExampleAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $form): void
    {
        $form
            ->add('name', null)
            ->add('description', null);
        ;

        $builder = $form->getFormBuilder();
        $builder->setDataMapper(new ExampleDataMapper());
    }

    // ...
}
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Measure & Improve Symfony Code Performance

    Measure & Improve Symfony Code Performance

    Show your Symfony expertise

    Show your Symfony expertise

    Version:

    Table of Contents

    • Pre-requisites
    • The recipe
      • Example Entity
      • DataMapper
      • Admin class

    Symfony footer

    Avatar of Michał Jusięga, a Symfony contributor

    Thanks Michał Jusięga for being a Symfony contributor

    2 commits • 4 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony