Doctrine2 + symfony2使用FK删除历史记录表

粉碎

我有2张桌子。我们说一个组织,第二个是organization_history。然后我有一个带有具体动作的表actions_history,...但是在这种情况下,它并不是那么重要。在组织历史记录中,我保留修订和organizationId。一切正常,直到删除表组织。我的想法是将所有动作保留在历史记录表中。在INSERT,UPDATE和DELETE操作上。但是问题是当我尝试删除组织表时。我得到以下输出:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`database`.`base_organizationsHistory`, CONSTRAINT `FK_EAF457A532C8A3DE` FOREIGN KEY (`organization_id`) REFERENCES `base_organizations` (`id`))

1)是否可以只删除组织表并保留历史表并忽略外键。还是存在一个不同甚至清晰的解决方案?

2)我也在考虑生命周期回调。在INSERT和UPDATE动作中,我可以轻松地连接表,但是当我可以在delete动作中创建历史表时,就无法使用postRemove回调,因为那样的话,我就没有用于复制到历史记录的旧数据。而且,如果我使用preRemove回调,它还不太清楚。是否存在一些更好的主意呢?

Organization.orm.yml:

BaseBundle\Entity\Organization:
type: entity
table: base_organizations
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    name:
        type: string
        length: 128
    type:
        type: string
        length: 64
oneToMany:
    organizationHistory:
        targetEntity: OrganizationHistory
        mappedBy: organization
        nullable: true

lifecycleCallbacks:
  postPersist: [saveInsertHistory]
  postUpdate: [saveUpdateHistory]
  preRemove: [saveDeleteHistory]

和OrganizationHistory.orm.yml

BaseBundle\Entity\OrganizationHistory:
type: entity
table: base_organizationsHistory
uniqueConstraints:
    organization_history_idx:
        columns: [ organizationId, revision ]
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    name:
        type: string
        length: 128
    type:
        type: string
        length: 64
    revision:
        type: integer
        nullable: false
    organizationId:
        type: integer
        nullable: false
    createdAt:
        type: datetime
        nullable: false
    updatedAt:
        type: datetime
        nullable: false
manyToOne:
    organization:
        targetEntity: Organization
        inversedBy: organizationHistory
        nullable: true
lifecycleCallbacks:
     preUpdate: [ setUpdateTimestamp ]
     prePersist: [ setCreationTimestamp, setUpdateTimestamp ]
汤玛斯·蒂本斯基

您不能从一个表中删除数据,也希望将与组织相关的数据存储在另一表中。这就是为什么要建立关系的原因。但是,您正在使用symfony,请执行“软删除”,这只会将实体标记为已删除,但是数据仍将存在于您的数据库中。

也许您可以从这里开始https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/softdeleteable.md

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Doctrine2 + Symfony2保留每次插入,更新,删除的历史记录

来自分类Dev

Doctrine2 NEW语法

来自分类Dev

Doctrine2 Paginator,getArrayResult

来自分类Dev

在Doctrine2中的间隔

来自分类Dev

Doctrine2多级继承

来自分类Dev

使用QueryBuilder的Symfony2 / Doctrine2 innerJoin

来自分类Dev

Symfony2 Doctrine2使用集合-表演

来自分类Dev

symfony2 doctrine2与实体的批处理错误

来自分类Dev

Symfony2,Doctrine2,MySql,查看表

来自分类Dev

Symfony2 + Doctrine2中的CakePHP模型行为?

来自分类Dev

symfony2 doctrine2与实体的批处理错误

来自分类Dev

在Symfony2上的Doctrine2 YML映射

来自分类Dev

列的总和-Doctrine2 / Symfony2

来自分类Dev

symfony2 doctrine2不刷新新实体

来自分类Dev

Symfony2,Doctrine2,实体映射

来自分类Dev

doctrine2 symfony2关系实体

来自分类Dev

COUNT = 0的Doctrine2 DQL问题

来自分类Dev

通过Doctrine2加入条件IN

来自分类Dev

Doctrine2结果缓存ID

来自分类Dev

过滤与Doctrine2的多对多关联

来自分类Dev

doctrine2按ID数组排序

来自分类Dev

Doctrine2在“ group by”之前的“ order by”

来自分类Dev

Doctrine2 PostgreSQL模式限制

来自分类Dev

Pagerfanta和Doctrine2 COUNT优化

来自分类Dev

Doctrine2与条件的关联映射

来自分类Dev

Doctrine2 PostgreSQL模式限制

来自分类Dev

制定复杂的Doctrine2 DQL查询

来自分类Dev

doctrine2 ORM和命名策略

来自分类Dev

通过Doctrine2与条件IN结合