简体   繁体   中英

Sonata Admin: How to represent a Many to Many in the edit form when the associated entity has thousands of records?

In my entities, I have a many to many relationship between roles and users

/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
 */
protected $users;

In the edit form for the Role entity I'd like to be able to see the users with that role, and to add and remove users too, so I added a users field in configureFormFields

protected function configureFormFields(FormMapper $formMapper) {
    $formMapper
        ->add('name')
        ->add('description')
        ->add('users');
}

The problem is that Sonata's approach is very naive: to render this form, it executes one query to retrieve the fields of the role, one to retrieve the fields of the users with this role, and one to retrieve the fields of ALL THE USERS IN THE DATABASE !!!

As I have more than 20,000 users in the database, this uses more than 250MB of memory .

Is there a way to instruct Sonata to show a paginated list with search or something like that?

Could pcdummy/AjaxCompleteBundle be of any interest? I have just stumbled upon this today.

And I would also suggest creating separate intermediate users_roles entity (and an admin for it). Then, using sonata_type_collection, you can add Group:userGroups field into Group admin. Remember to not include group field in UsersGroupAdmin form config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM