【问题标题】:Doctrine Soft Delete - still return deleted entity in all contexts except oneDoctrine Soft Delete - 仍然在所有上下文中返回已删除的实体,除了一个
【发布时间】:2017-09-14 14:42:37
【问题描述】:

我有一个 Doctrine 实体 (FieldOption),它代表表单中选择字段中的一个选项(例如 RedGreenBlue)。用户在任何时候都可以选择从字段中删除选项(例如删除Green),为此我已使用 Gedmo 将实体设置为可软删除,效果很好。

但是,当针对至少一个表单提交而存储的选项被删除时,该提交实体(与 FieldOption 实体有关系)无法加载,因为 FieldOption 被过滤掉了。真的,我只希望在返回用于呈现表单的数据时出现软删除功能 - 查看链接到该实体的现有记录应该仍然能够默认加载软删除选项。

有没有办法在通过另一个实体检索时禁用此过滤器,但确保在直接加载选项列表时启用过滤器?

【问题讨论】:

  • 也许您可以使用Doctring SQLFilter (docs.doctrine-project.org/projects/doctrine-orm/en/latest/…) 编写自己的过滤器?
  • 你解决了吗?
  • 我不认为我做过@Nukeface - 考虑一下,我不认为这真的是软删除的有效用法(因为它应该被视为与真正的删除相同)所以我可能会实现一个自定义属性和学说过滤器来处理这个

标签: php doctrine-orm soft-delete


【解决方案1】:

Sepultura 评论的链接提供了一些示例,说明如何启用/禁用过滤器和编写您自己的自定义过滤器。

From the docs

过滤器示例:

<?php
namespace Example;
use Doctrine\ORM\Mapping\ClassMetaData,
    Doctrine\ORM\Query\Filter\SQLFilter;

class MyLocaleFilter extends SQLFilter
{
    public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
    {
        // Check if the entity implements the LocalAware interface
        if (!$targetEntity->getReflectionClass()->implementsInterface('LocaleAware')) {
            return "";
        }

        return $targetTableAlias.'.locale = ' . $this->getParameter('locale'); // getParameter applies quoting automatically
    }
}

文档还说使用以下代码行添加此过滤器(以便您可以启用/禁用它):

$config->addFilter("locale", "\Doctrine\Tests\ORM\Functional\MyLocaleFilter");

然而,如果你想在任何地方使用这个过滤器,你必须把这行代码放在你想要的任何地方,并且还可以访问$config(其中包含来自的Configuration对象教义)。

在 Zend Framework 2 和 3 中,您可以将以下配置添加到 core/mvc 模块或应用程序配置中:

'doctrine' => [
    'configuration' => [
        'orm_default' => [
            'filters' => [
                'locale' => MyLocaleFilter::class,
            ],
        ],
    ],
],

上述代码行和配置启用此过滤器,要在特定位置禁用,请在要禁用的位置使用以下行(根据文档(第一个启用和配置)):

<?php
$filter = $em->getFilters()->enable("locale");
$filter->setParameter('locale', 'en');

// Disable it
$filter = $em->getFilters()->disable("locale");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 2019-06-05
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多