DoctrineExtensions SoftDeleteable的配置条目:gedmo / doctrine-extensions

时间:2015-04-22 13:12:32

标签: symfony doctrine-orm

我尝试使用gedmo/doctrine-extensionssoftdelete选项,但出于某种原因,当我致电romove()时,数据库中的记录会被删除而不是更新deletedAt字段。

here中,doc告诉我们使用以下命令更新配置

  

组$ config-> addFilter('软可删除&#39 ;,   ' Gedmo \ SoftDeleteable \过滤\ SoftDeleteableFilter&#39);

这只是我尝试过的一个例子:

# app/config/config.yml
doctrine:
    orm:
        entity_managers:
            default:
                filters:
                    softdeleteable:
                        class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                        enabled: true

参考文献(仅其中一些):

所以简单来说问题是,如何在config.yml中配置它?

CONTROLLER

public function delete($id)
{
    $profile = $this->profileRepository->findOneBy(['id' => $id]);

    if (!$profile instanceof Profile) {
        throw new ........
    }

    $this->entityManager->remove($profile);
    $this->entityManager->flush();

    return true;
}

ENTITY

use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity()
 * @ORM\Table(name="profile")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 */
class Profile
{
    /**
     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
     */
    private $deletedAt;
    ......
}

COMPOSER.JSON

"require": {
    "symfony/symfony": "2.6.*",
    "doctrine/orm": "~2.2,>=2.2.3",
    "doctrine/doctrine-bundle": "~1.2",
    "gedmo/doctrine-extensions": "2.3.*@dev",
    ......
},

CONFIG.YML

doctrine:
    dbal:
      default_connection: front
      connections:
        front:
          driver:   %database_driver%
          host:     %database_host%
          ........
        back:
          driver:   %database_driver%
          host:     %database_host%
          ........


    orm:
        auto_generate_proxy_classes: %kernel.debug%
        default_entity_manager:      front

        entity_managers:
            front:
                connection:       front
                mappings:
                    MyWebsiteBundle:
                        dir:      Entity
                    FOSUserBundle: ~

            back:
                connection:       back

映射信息:

inanzzz@inanzzz:/var/www/html/local$ php app/console doctrine:mapping:info
Found 8 mapped entities:
[OK]   My\Bundle\Entity\AbstractMerchantProfile
[OK]   My\Bundle\Entity\AbstractIntegration
[OK]   My\Bundle\Entity\APIConsumer
[OK]   My\Bundle\WebsiteBundle\Entity\User
[OK]   My\Bundle\WebsiteBundle\Entity\Profile
[OK]   My\Bundle\WebsiteBundle\Entity\Integration
[OK]   FOS\UserBundle\Model\Group
[OK]   FOS\UserBundle\Model\User

2 个答案:

答案 0 :(得分:2)

这是我配置它的方式

doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true
        filters:
            softdeleteable:
                class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                enabled: true

答案 1 :(得分:2)

<强>解决方案:

composer.json中包含stof/doctrine-extensions-bundle

"stof/doctrine-extensions-bundle": "1.2.*@dev",

套餐为here。文档为here

在AppKernel中启用bundle: new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle()

由于我在config.yml中有多个实体管理器,我做了:

stof_doctrine_extensions:
    orm:
        em1:
            softdeleteable: true


doctrine:
    dbal:
      default_connection: em1
      connections:
        em1:
          driver:   %database_driver%
          host:     %database_host%
          .......

        em2:
          driver:   %database_driver%
          host:     %database_host%
          .......

        em3:
          driver:   %mws_database_driver%
          host:     %mws_database_host%
          .......

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        default_entity_manager:      em1

        entity_managers:
            em1:
                connection:       em1
                mappings:
                    MyWebsiteBundle:
                        dir:      Entity
                    FOSUserBundle: ~
                filters:
                    softdeleteable:
                        class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                        enabled: true

            em2:
                connection:       em2

            em3:
                connection:       em3