使用可通过api平台进行软件删除的Doctrine扩展

时间:2018-06-19 14:53:05

标签: symfony soft-delete doctrine-extensions api-platform.com

我正在使用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

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

在您的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类。