我正在尝试使用 gedmo/doctrine-extension 的选项softdelete
但由于某种原因,当我调用 romove()
时,数据库中的记录被删除而不是更新deletedAt
字段。
在这里,doc 告诉我们用以下方法更新配置:
$config->addFilter('soft-deleteable', 'Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter');
这只是我尝试过的例子之一:
# app/config/config.yml
doctrine:
orm:
entity_managers:
default:
filters:
softdeleteable:
class: GedmoSoftDeleteableFilterSoftDeleteableFilter
enabled: true
参考文献(仅其中的几个):
- DoctrineExtensions SoftDeleteable
- http://knplabs.com/en/blog/gedmo-doctrine-extensions-on-symfony2
- 无法在 Symfony2 中启用软删除 - 无法识别的选项"过滤器"
所以问题很简单,如何在 config.yml 中配置它?
控制器
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;
}
实体
use GedmoMappingAnnotation as Gedmo;
/**
* @ORMEntity()
* @ORMTable(name="profile")
* @GedmoSoftDeleteable(fieldName="deletedAt")
*/
class Profile
{
/**
* @ORMColumn(name="deletedAt", type="datetime", nullable=true)
*/
private $deletedAt;
......
}
作曲家。杰伦
"require": {
"symfony/symfony": "2.6.*",
"doctrine/orm": "~2.2,>=2.2.3",
"doctrine/doctrine-bundle": "~1.2",
"gedmo/doctrine-extensions": "2.3.*@dev",
......
},
配置。扬明
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] MyBundleEntityAbstractMerchantProfile
[OK] MyBundleEntityAbstractIntegration
[OK] MyBundleEntityAPIConsumer
[OK] MyBundleWebsiteBundleEntityUser
[OK] MyBundleWebsiteBundleEntityProfile
[OK] MyBundleWebsiteBundleEntityIntegration
[OK] FOSUserBundleModelGroup
[OK] FOSUserBundleModelUser
这就是我的配置方式
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: GedmoSoftDeleteableFilterSoftDeleteableFilter
enabled: true
解决方案:
包含在 composer.json 中的stof/doctrine-extensions-bundle
"stof/doctrine-extensions-bundle": "1.2.*@dev",
包在这里。文档在这里。
在应用程序内核中启用捆绑包: new StofDoctrineExtensionsBundleStofDoctrineExtensionsBundle()
由于我在 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: GedmoSoftDeleteableFilterSoftDeleteableFilter
enabled: true
em2:
connection: em2
em3:
connection: em3