如何根据某些字段值从集合中隐藏项目? [英] How to hide item from collection depending on some field value?

查看:23
本文介绍了如何根据某些字段值从集合中隐藏项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我覆盖(自定义操作和服务)我的应用程序的 DELETE 操作以避免从数据库中删除数据.我所做的是更新一个字段值:isDeleted === true.

I override (custom operation and service) the DELETE operation of my app to avoid deleting data from DB. What I do is I update a field value: isDeleted === true.

这是我的控制器:

class ConferenceDeleteAction extends BaseAction
{
    public function __invoke(EntityService $entityService, Conference $data)
    {
        $entityService->markAsDeleted($data, Conference::class);
    }

...

我的服务:

class EntityService extends BaseService
{
    public function markAsDeleted(ApiBaseEntity $data, string $className)
    {
        /**
         * @var ApiBaseEntity $entity
         */
        $entity = $this->em->getRepository($className)
            ->findOneBy(["id" => $data->getId()]);
        if ($entity === null || $entity->getDeleted()) {
            throw new NotFoundHttpException('Unable to find this resource.');
        }

        $entity->setDeleted(true);
        if ($this->dataPersister->supports($entity)) {
            $this->dataPersister->persist($entity);
        } else {
            throw new BadRequestHttpException('An error occurs. Please do try later.');
        }
    }

}

如何在 GET 动词的集合中隐藏已删除"的项目(从结果中过滤它们,使它们不可见)?

How can I hide the "deleted" items from collection on GET verb (filter them from the result so that they aren't visible) ?

这是我对 GET 动词的操作,我不知道如何处理:

Here is my operation for GET verb, I don't know how to handle this :

class ConferenceListAction extends BaseAction
{
    public function __invoke(Request $request, $data)
    {
        return $data;
    }
}

推荐答案

不鼓励在 文档.您正在使用 Doctrine ORM,因此您可以使用自定义 Doctrine ORM 扩展:

Custom controllers are discouraged in the docs. You are using Doctrine ORM so you can use a Custom Doctrine ORM Extension:

// api/src/Doctrine/ConferenceCollectionExtension.php

namespace App\Doctrine;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\Conference;
use Doctrine\ORM\QueryBuilder;

final class CarCollectionExtension implements QueryCollectionExtensionInterface
{
    public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null): void
    {
        if ($resourceClass != Conference::class) return;

         $rootAlias = $queryBuilder->getRootAliases()[0];
         $queryBuilder->andWhere("$rootAlias.isDeleted = false OR $rootAlias.isDeleted IS NULL);
    }
}

这将自动与使用方法 GET 的集合操作的任何过滤器、排序和分页结合.

This will automatically be combined with any filters, sorting and pagination of collection operations with method GET.

您可以通过在 if 语句中添加以下内容来使此扩展特定于某个操作:

You can make this Extension specific to an operation by adding to the if statement something like:

|| $operationName == 'conference_list'

如果您不使用自动配置,则必须注册自定义扩展:

If you're not using the autoconfiguration, you have to register the custom extension:

# api/config/services.yaml
services:

    # ...

    'App\Doctrine\ConferenceCollectionExtension':
        tags:
            - { name: api_platform.doctrine.orm.query_extension.collection }

如果您还想为项目操作添加标准,请参阅关于扩展的文档

If you also want to add a criterium for item operations, see the docs on Extensions

这篇关于如何根据某些字段值从集合中隐藏项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆