简体   繁体   English

Sonata Admin:当关联实体具有数千条记录时,如何在编辑表单中表示“多对多”?

[英]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 Role实体的编辑表单中,我希望能够看到具有该角色的用户,并且也能够添加和删除用户,因此我在configureFormFields添加了一个users字段。

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 !!! 问题在于Sonata的方法非常幼稚:呈现此表单,它执行一个查询以检索角色的字段,一个查询以检索具有该角色的用户的字段,并检索所有用户的字段数据库 !!!

As I have more than 20,000 users in the database, this uses more than 250MB of memory . 由于我在数据库中拥有超过20,000个用户,因此使用了250MB以上的内存

Is there a way to instruct Sonata to show a paginated list with search or something like that? 有没有一种方法可以指示Sonata显示带有搜索或类似内容的分页列表?

Could pcdummy/AjaxCompleteBundle be of any interest? pcdummy / AjaxCompleteBundle对您有什么好处吗? I have just stumbled upon this today. 我今天偶然发现了这一点。

And I would also suggest creating separate intermediate users_roles entity (and an admin for it). 而且我还建议创建单独的中间users_roles实体(以及它的管理员)。 Then, using sonata_type_collection, you can add Group:userGroups field into Group admin. 然后,使用sonata_type_collection,可以将Group:userGroups字段添加到Group admin。 Remember to not include group field in UsersGroupAdmin form config. 切记不要在UsersGroupAdmin表单配置中包括group字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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