api-平台过滤掉软删除项
api-platform filter out the soft deleted items
我已经使用 softdelete 为我的商店实体设置了软删除。
这是我在 doctrine.yml
:
中的过滤器设置
doctrine:
# ...
orm:
# ...
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
所以当我点击例如URL /stores
只返回活跃的商店,但如果我将配置更改为 enabled: false
它会给我所有结果,包括已删除的项目,这是正确的。
现在我想要实现的是从前端传递一个查询参数,例如 /stores?deleted=1
然后我想获取所有数据,如果没有 deleted=1
只找到活动项目
为什么不创建一个使用 Request
对象和 Doctrine 的实体管理器的事件侦听器,并禁用此过滤器?像这样:
class FilterListener implements EventSubscriberInterface
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents(): array
{
return [
RequestEvent::class => 'onKernelRequest',
];
}
public function onKernelRequest(RequestEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
if ($request->query->getBoolean('deleted')) {
$this->entityManager->getFilters()->disable('softdeleteable');
}
}
}
由于@nico-haase 的回答对我不起作用,我使用 Symfony 5.3 和 Api Plarform 2.6 发布了适用于我的类似解决方案:
class FilterSubscriber implements EventSubscriberInterface
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['modifyFilters', EventPriorities::PRE_READ],
];
}
public function modifyFilters(RequestEvent $event)
{
$request = $event->getRequest();
// show deleted entities in case deleted param is provided
if ($request->query->getBoolean('deleted')) {
$this->entityManager->getFilters()->disable('softdeleteable');
}
}
}
我已经使用 softdelete 为我的商店实体设置了软删除。
这是我在 doctrine.yml
:
doctrine:
# ...
orm:
# ...
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
所以当我点击例如URL /stores
只返回活跃的商店,但如果我将配置更改为 enabled: false
它会给我所有结果,包括已删除的项目,这是正确的。
现在我想要实现的是从前端传递一个查询参数,例如 /stores?deleted=1
然后我想获取所有数据,如果没有 deleted=1
只找到活动项目
为什么不创建一个使用 Request
对象和 Doctrine 的实体管理器的事件侦听器,并禁用此过滤器?像这样:
class FilterListener implements EventSubscriberInterface
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents(): array
{
return [
RequestEvent::class => 'onKernelRequest',
];
}
public function onKernelRequest(RequestEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
if ($request->query->getBoolean('deleted')) {
$this->entityManager->getFilters()->disable('softdeleteable');
}
}
}
由于@nico-haase 的回答对我不起作用,我使用 Symfony 5.3 和 Api Plarform 2.6 发布了适用于我的类似解决方案:
class FilterSubscriber implements EventSubscriberInterface
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['modifyFilters', EventPriorities::PRE_READ],
];
}
public function modifyFilters(RequestEvent $event)
{
$request = $event->getRequest();
// show deleted entities in case deleted param is provided
if ($request->query->getBoolean('deleted')) {
$this->entityManager->getFilters()->disable('softdeleteable');
}
}
}