Discuss your project
PHP symfony Sonata

Symfony 4 / Sonata: create a ManyToMany administration

We will develop here all the manipulations to build a many to many interface via Sonata.We will take as an example a zone management with a connection by departments. Each zone is thus constituted of multiple connections with the department table.In MysqlW...

Symfony 4 / Sonata: create a ManyToMany administration

We will develop here all the manipulations to build a many to many interface via Sonata.
We will take as an example a zone management with a connection by departments. Each zone is thus constituted of multiple connections with the department table.

In MysqlWorkbench, it looks like this:

Sélection_123

To generate the entities, we use the following command:

php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
Sélection_124

Next, we create the setters and getters in our entities using the command:

php bin/console make:entity --regenerate App
Sélection_125-1

Now all that's left is to generate our Sonata CRUD without forgetting to clear the cache.

php bin/console make:sonata:admin App/Entity/ZxZone
php bin/console cache:clear
Sélection_126

Our interface will be created and the service as well. We will still have to perform a few manipulations.

1 – Remove the ID from our record creation form and add our department field

//ajouter le use suivant
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

//supprimez l'id, et ajouter la liaison département
  protected function configureFormFields(FormMapper $formMapper): void
    {
       $formMapper
           //->add('id')
            ->add('label')
            ->add('code')
            ->add('departement', EntityType::class, [
                'class' => 'App\Entity\Departement',
                'choice_label' => 'label',
                'label' => 'Départements',
                'required' => false,
                'by_reference' => false,
                'multiple' => true,
                'expanded' => false,
            ]);
    }

In the department definition we explicitly ask to display the label field in our list, via the "choice_label" parameter.

This allows us to display a nicer label than just the id. But for this, we must add the following methods in the department entity.

//Permet d'afficher un nom plus sympa dans les messages du CRUD
    public function __toString(): ?string
    {
        return $this->id." : ".$this->nomClairRiche;
    }
//Permet d'afficher un label dans notre liste de configuration
    public function getLabel(): ?string
    {
        return $this->id." : ".$this->nomClairRiche;
    }
on2many

Adapt the example to the installed version

The Symfony and Sonata examples remain tied to the versions indicated in the article. Symfony 4.x and Symfony 6.2 are no longer maintained: for a current project, use a maintained branch and the documentation that exactly matches your dependencies.

php bin/console about
composer show symfony/framework-bundle sonata-project/admin-bundle

Before resuming the code, check the signatures, services, routes, and templates concerned. Then add a functional test covering permissions, HTTP code, and the visible result.

Reference: maintained Symfony versions.

Share this article