src/Admin/UsuarioAdmin.php line 13

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Admin;
  4. use Sonata\AdminBundle\Admin\AbstractAdmin;
  5. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  6. use Sonata\AdminBundle\Datagrid\ListMapper;
  7. use Sonata\AdminBundle\Form\FormMapper;
  8. use Sonata\AdminBundle\Show\ShowMapper;
  9. final class UsuarioAdmin extends AbstractAdmin
  10. {
  11.     protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
  12.     {
  13.         $datagridMapper
  14.             ->add('id')
  15.             ->add('nome')
  16.             ->add('email')
  17.             ;
  18.     }
  19.     protected function configureListFields(ListMapper $listMapper): void
  20.     {
  21.         $listMapper
  22.             ->add('id')
  23.             ->add('nome')
  24.             ->add('email')
  25.             ->add('_action'null, [
  26.                 'actions' => [
  27.                     'show' => [],
  28.                     'edit' => [],
  29.                     'delete' => [],
  30.                 ],
  31.             ]);
  32.     }
  33.     protected function configureFormFields(FormMapper $formMapper): void
  34.     {
  35.         $formMapper
  36.             ->add('nome')
  37.             ->add('email')
  38.             ;
  39.     }
  40.     protected function configureShowFields(ShowMapper $showMapper): void
  41.     {
  42.         $showMapper
  43.             ->add('id')
  44.             ->add('nome')
  45.             ->add('email')
  46.             ->add('roles')
  47.             ;
  48.     }
  49.     
  50.     public function preRemove($object): void
  51.     {
  52.         parent::preRemove($object);
  53.         
  54.         $em $this->getModelManager()->getEntityManager(\App\Entity\Usuario::class);
  55.         
  56.         // Remove todos os contratos de curso relacionados
  57.         $contratos $em->getRepository(\App\Entity\ContratoCursoAluno::class)
  58.             ->findBy(['usuario' => $object]);
  59.         
  60.         foreach ($contratos as $contrato) {
  61.             $em->remove($contrato);
  62.         }
  63.         
  64.         // Remove todos os alunos relacionados
  65.         $alunos $em->getRepository(\App\Entity\UAluno::class)
  66.             ->findBy(['usuario' => $object]);
  67.         
  68.         foreach ($alunos as $aluno) {
  69.             $em->remove($aluno);
  70.         }
  71.         
  72.         $em->flush();
  73.     }
  74. }