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:
To generate the entities, we use the following command:
php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
![Sélection_124 Sélection_124](/uploads/media/default/0001/01/28e3845897471cdf23957fe9678c09069efda43a.png)
Next, we create the setters and getters in our entities using the command:
php bin/console make:entity --regenerate App
![Sélection_125-1 Sélection_125-1](/uploads/media/default/0001/01/2cbeca4d5b23eda9bcd21d46a449077c1c07ac65.png)
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 Sélection_126](/uploads/media/default/0001/01/a978f30535b3f40585ae8a6b02428148bab4260e.png)
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 on2many](/uploads/media/default/0001/01/7c35f3ef69aaeaeed88701bbe8c1ee4a11e65096.gif)