我有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] 删除。
我来说两句