gpt4 book ai didi

php - EasyAdmin 3 : limit data to the logged-in user still shows other data in form dropdowns

转载 作者:行者123 更新时间:2023-12-05 04:59:40 32 4
gpt4 key购买 nike

我正在使用 Symfony 5。

我希望每个登录用户在 EasyAdmin 3 中都有自己的空间,这样任何用户都不会看到其他用户的记录。我将用户与数据库中的每个表一起存储。

对于简单的 ListView ,我设法使用 AbstractCrudController 的扩展使其工作:

<?php
namespace App\Controller\Admin;

use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository;

abstract class CustomCrudController extends AbstractCrudController
{
public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{
$qb = $this->get(EntityRepository::class)->createQueryBuilder($searchDto, $entityDto, $fields, $filters);
$qb->andWhere('entity.user = :user');
$qb->setParameter('user', $this->getUser());
return $qb;
}
}

我还通过 EventSubscriber 存储/检查用户。

问题是,某些表单与另一个实体有关系(如 AssociationField::new('food')),并且在填充下拉列表时它会忽略我的新功能。所以您会看到属于另一个用户的记录。

如何覆盖这些下拉菜单以仅显示属于当前用户的数据?

最佳答案

我找到了解决方案:将自定义查询传递给 Symfony 的底层 EntityType 字段。

AssociationField::new('food')
->setRequired(true)
->setFormTypeOptions(['query_builder' => function (EntityRepository $em) {
return $em->createQueryBuilder('f')
->where('f.user = :user')
->orderBy('f.title', 'ASC')
->setParameter('user', $this->getUser())
;
}]),

关于php - EasyAdmin 3 : limit data to the logged-in user still shows other data in form dropdowns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63439187/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com