在 api-platform 上使用 Doctrine 扩展 softdeleteable

Using Doctrine extension softdeleteable with api-platform

我正在使用 Symfony 3.4 和 api 平台构建一个 API。我想在我的实体上使用软删除。我已经安装了 DoctrineExtensionsStofDoctrineExtensionsBundle.

config.yml:

doctrine:
    dbal:
        connections:
            default:
               […]

    orm:
        entity_managers:
            default:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                connection: default
                mappings:
                    […]
                filters:
                    softdeleteable:
                        class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                        enabled: true

还有我的实体:

<?php

namespace AppBundle\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * MyEntity
 *
 * @ORM\Table(name="MyEntity", schema="MyEntity")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\MyEntityRepository")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 * @ApiResource
 */
class MyEntity
{
    /**
     * @var \DateTime
     * @ORM\Column(name="deleted_at", type="datetime")
     */
    private $deletedAt;

    […]

这是行不通的。我知道我需要配置一些东西(即 EventManager),但我不知道如何配置。 这是我尝试创建实体时遇到的错误

Listener "SoftDeleteableListener" was not added to the EventManager!

我想我已经完成了页面说明的所有内容:StofDoctrineExtensionsBundle documentation

如有任何帮助,我们将不胜感激。

在您的config.yml

尝试以下配置
doctrine:
    orm:
        entity_managers:
            default:
                naming_strategy: doctrine.orm.naming_strategy.underscore
                connection: default
                mappings:
                    […]
                filters:
                    softdeleteable:
                        class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                        enabled: true

stof_doctrine_extensions:
    default_locale: %locale%
    orm:
        default:
            softdeleteable: true

注意:我的配置如下:

orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    entity_managers:
      default:
        auto_mapping: true
        filters:
            softdeleteable:
              class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
              enabled: true

您似乎正在自定义 mappings,因此请确保您正确地自动加载了 SoftDeleteable 类。