我正在尝试使用softdelete
的选项gedmo/学说扩展 https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/softdeleteable.md但由于某种原因当我打电话时romove()
,数据库中的记录被删除而不是更新deletedAt
field.
In here https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/softdeleteable.md#setup-and-autoloading,文档告诉我们更新配置:
$config->addFilter('软删除',
'Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter');
这只是我尝试过的示例之一:
# app/config/config.yml
doctrine:
orm:
entity_managers:
default:
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
参考文献(仅部分):
- DoctrineExtensions 软删除 https://stackoverflow.com/questions/10056661/doctrineextensions-softdeleteable
- http://knplabs.com/en/blog/gedmo-doctrine-extensions-on-symfony2 http://knplabs.com/en/blog/gedmo-doctrine-extensions-on-symfony2
- 无法在 Symfony2 中启用 SoftDeleteable - 无法识别的选项“过滤器” https://stackoverflow.com/questions/10374449/cant-enable-softdeleteable-in-symfony2-unrecognized-options-filters
那么问题简单来说,如何在 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;
}
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;
......
}
作曲家.JSON
"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] 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