我正在用 Symfony 3.4 和 api-platform 构建一个 API。我想对我的实体使用软删除。我已经安装 DoctrineExtensions
和 StofDoctrineExtensionsBundle
.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/