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

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

问题描述

我重写(自定义操作和服务)我的应用程序的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天全站免登陆