symfony - 在 api-platform 中使用 Doctrine 扩展软删除

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

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

    […]

这是行不通的。我知道我需要配置一些东西(即 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 类。

关于symfony - 在 api-platform 中使用 Doctrine 扩展软删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50931363/

相关文章:

indexing - 软删除 - 使用 IsDeleted 标志还是单独的连接表?

Symfony2/学说 : Get the field(s) that changed after "Loggable" entity changed

symfony - Gedmo Doctrine Extensions 中的个人翻译是什么?

forms - 在 Symfony 2 中创建表单网格

php - "Temporary failure in name resolution": Symfony 5. 3 使用 Doctrine 和 Docker 连接到 MariaDB

php - Make Doctrine 以原始顺序添加实体

java - 软删除包位置

php - 在 symfony 中加载资源

php - Propel 是否知道对象何时被软删除,以便子实体仍然可以显示其已删除的父实体?

php - 如何在 Zend Framework 2 中配置学说扩展?