2

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?

zootropo
  • 2,441
  • 3
  • 31
  • 48
  • 1
    See the accepted answer here http://stackoverflow.com/questions/8037449/sonataadminbundle-form-field-query – Tocacar Jul 25 '13 at 12:45
  • 1
    Basically, you want to pass a query to the user entity field and only display a subset of your users – Tocacar Jul 25 '13 at 12:46
  • Thank you @Tocacar. I thought about that, but then, the admin wouldn't have a way to assign existing users to that role – zootropo Jul 26 '13 at 06:13
  • Maybe I could use something like `sonata_type_collection` or `sonata_type_model_list` instead of `sonata_type_model`? but these two don't have a `query` option... :( – zootropo Jul 26 '13 at 06:56

1 Answers1

1

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.

TautrimasPajarskas
  • 2,686
  • 1
  • 32
  • 40
  • It seems like AjaxCompleteBundle loads all the instances anyways and it just hides those using JavaScript. I suppose I will have to convert the relationship from a N:M to two 1:N as you suggests, but I don't know how that will affect Symfony's security component. Thank you for your answer Tautrimas – zootropo Jul 30 '13 at 07:48