问题描述
我尝试使用 EasyAdmin,它看起来非常棒.我想做这个,但我没有找到方法.
I try to use EasyAdmin, which looks very wonderful.I would like to make this, but I don't find How.
我有一些项目",它们可以有很多经理"(我在这两个实体之间有一个多对多的关系).当经理使用EasyAdmin登录时,我想在项目列表中只列出他的项目.我尝试使用过滤器,但我不知道将什么作为约束来限制我的用户项目.
I have some "projects", which can have many "managers" (I have a ManyToMany relationship between these two entities).When a manager logs in with EasyAdmin, I would like to list only his projects in the list of projects. I try to use the filters, but I do not see what to put as a constraint to have only my user's projects.
你有什么想法吗?可以这样做吗?
Do you have an idea? Is it possible to do that?
easy_admin:
site_name: 'PTUT MMI'
list:
actions: ['show']
formats:
date: 'd/m/Y'
time: 'H:i'
datetime: 'd/m/Y H:i:s'
user:
display_name: true
display_avatar: false
design:
menu:
# only users with the ROLE_SUPER_ADMIN role will see this menu item
- { entity: 'Projet', permission: ['ROLE_ADMIN',] }
- { icon: user-cog, label: 'Mes Projets', entity: ProjetsResponsable, params: { action: list, filters: { responsables: { comparison: like, value: [1] } } } }
- { entity: 'User', permission: ['ROLE_ADMIN'] }
- { entity: 'Etudiant', permission: ['ROLE_RESPONSABLE'] }
entities:
# # List the entity class name you want to manage
## Entités pour l'admin
Projet:
class: App\Entity\Projet
User:
class: App\Entity\User
## Entités pour le responsable
ProjetsResponsable:
class: App\Entity\Projet
list:
filters:
- { property: responsables, type: array }
Etudiant:
class: App\Entity\User
我尝试在数组中转换 Responsables(经理),它是一个集合,并在菜单中使用 like 过滤(示例为 1,但通常与连接的用户一起使用).
I have try to transform Responsables (manager) in array, it is a collection, and filter with like in menu (with 1 for the example, but normaly with the connected user).
谢谢,
推荐答案
一些有用的信息,您可以从 文档.
some useful information you can get from the documentation.
因此,我完成了类似的任务,创建自定义控制器并覆盖 createListQueryBuilder
方法以获取每个用户的自定义列表.因此,在您的示例中,它将是这样的:
So, I have done similar task creating custom Controller and overriding createListQueryBuilder
method to get custom list for each user. So, in your example it will be something like this:
class ProjetsResponsable extends BaseEasyAdminController
{
protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
{
if (null === $dqlFilter) {
$dqlFilter = sprintf('entity.user = %s', $this->getUser()->getId());
} else {
$dqlFilter .= sprintf(' AND entity.user = %s', $this->getUser()->getId());
}
return parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
}
}
然后在配置中:
entities:
ProjetsResponsable:
class: App\Entity\Projet
controller: App\Controller\ProjetsResponsable
list:
fields:
#fields...
这篇关于如何过滤具有多对多关系的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!