使用Doctrine扩展软删除与api-platform

10

我正在使用Symfony 3.4和api-platform构建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;

    […]
这个不起作用。 我知道我需要配置某些东西(即事件管理器),但我不知道如何操作。在尝试创建实体时,以下是我收到的错误消息: "Listener“ SoftDeleteableListener”未添加到EventManager中!" 我认为我已经按照页面上的说明完成了所有步骤:StofDoctrineExtensionsBundle文档。任何帮助将不胜感激。
1个回答

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接