Search code examples
phpsymfonysymfony5easyadmineasyadmin3

Symfony 5 - Easy Admin 3: Heavy load on AssociationField when the associated entity have so many data


I have the following CrudController:

<?php

namespace App\Controller\Admin\Core;

use App\Entity\Core\Role;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\SlugField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

class RoleCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Role::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            TextField::new('name', 'Name')
                ->setRequired(true)
                ->setMaxLength(255)
                ->setHelp('The role name, prefix with: ROLE_'),
            SlugField::new('systemName', 'System Name')
                ->setRequired(true)
                ->setTargetFieldName('name')->setFormattedValue(function ($value) {
                    return strtoupper($value);
                }),
            TextEditorField::new('description', 'Description'),
            IntegerField::new('level', 'Role Level')->setHelp('Provide the role level'),
            AssociationField::new('subsOfRole', 'Parent Role'),
            ChoiceField::new('type', 'Role Relation Type')
                ->setChoices([
                    'User' => 1,
                    'Job Title' => 2,
                    'Unit' => 3,
                    'Office' => 4,
                    'Echelon' => 5,
                    'Office Type' => 6,
                    'user Group' => 7,
                    'Job Title + Unit' => 8,
                    'Job Title + Office' => 9,
                    'Job Title + Unit + Office' => 10,
                    'Job Title + Unit + Office Type' => 11
                ])
                ->setRequired(true),
            AssociationField::new('users', 'Users')
                ->setHelp('Must be filled when you choose User as Role Relation Type')
                ->hideOnIndex(),
            AssociationField::new('groups', 'Groups')
                ->setHelp('Must be filled when you choose Group as Role Relation Type')
                ->hideOnIndex(),
            AssociationField::new('jobTitles', 'Job Title')
                ->hideOnIndex(),
            AssociationField::new('units', 'Unit')
                ->hideOnIndex(),
            AssociationField::new('offices', 'Offices')
                ->hideOnIndex(),
            AssociationField::new('echelons', 'Echelons')
                ->hideOnIndex(),
            AssociationField::new('officeTypes', 'Office Types')
                ->hideOnIndex(),
        ];
    }
}

It runs well when we have small data, but when we test with tens of thousands data to User Entity/ other related entity, the CRUD page is so slow.

Is there any method to change the way associationField work? Or to improve the performance on user side (browser)?

Context: I use Symfony 5.3.9 and EasyAdmin 3.5.10 which is the latest version when I write this

Thank you


Solution

  • As suggested by Will B., I checked the autocomplete feature and tried it. That was the solution.

    My previous code became like (see the ->autocomplete() implementation):

    <?php
    
    namespace App\Controller\Admin\Core;
    
    use App\Entity\Core\Role;
    use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
    use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
    use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
    use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
    use EasyCorp\Bundle\EasyAdminBundle\Field\SlugField;
    use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
    use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
    
    class RoleCrudController extends AbstractCrudController
    {
        public static function getEntityFqcn(): string
        {
            return Role::class;
        }
    
        public function configureFields(string $pageName): iterable
        {
            return [
                TextField::new('name', 'Name')
                    ->setRequired(true)
                    ->setMaxLength(255)
                    ->setHelp('The role name, prefix with: ROLE_'),
                SlugField::new('systemName', 'System Name')
                    ->setRequired(true)
                    ->setTargetFieldName('name')->setFormattedValue(function ($value) {
                        return strtoupper($value);
                    }),
                TextEditorField::new('description', 'Description'),
                IntegerField::new('level', 'Role Level')->setHelp('Provide the role level'),
                AssociationField::new('subsOfRole', 'Parent Role')
                    ->autocomplete(),
                ChoiceField::new('type', 'Role Relation Type')
                    ->setChoices([
                        'User' => 1,
                        'Job Title' => 2,
                        'Unit' => 3,
                        'Office' => 4,
                        'Echelon' => 5,
                        'Office Type' => 6,
                        'user Group' => 7,
                        'Job Title + Unit' => 8,
                        'Job Title + Office' => 9,
                        'Job Title + Unit + Office' => 10,
                        'Job Title + Unit + Office Type' => 11
                    ])
                    ->setRequired(true),
                AssociationField::new('users', 'Users')
                    ->autocomplete()
                    ->setHelp('Must be filled when you choose User as Role Relation Type')
                    ->hideOnIndex(),
                AssociationField::new('groups', 'Groups')
                    ->autocomplete()
                    ->setHelp('Must be filled when you choose Group as Role Relation Type')
                    ->hideOnIndex(),
                AssociationField::new('jobTitles', 'Job Title')
                    ->autocomplete()
                    ->hideOnIndex(),
                AssociationField::new('units', 'Unit')
                    ->autocomplete()
                    ->hideOnIndex(),
                AssociationField::new('offices', 'Offices')
                    ->autocomplete()
                    ->hideOnIndex(),
                AssociationField::new('echelons', 'Echelons')
                    ->autocomplete()
                    ->hideOnIndex(),
                AssociationField::new('officeTypes', 'Office Types')
                    ->autocomplete()
                    ->hideOnIndex(),
            ];
        }
    }
    

    and now the load is good.

    Thank you