Symfony2删除实体,级联异常

时间:2013-09-14 16:48:39

标签: php symfony orm doctrine-orm

我有一个名为 Step2 的实体(有多个selectedriskassesments实体,它是一个带有额外字段的RiskAssestment实体):

......................
    manyToMany:
       selectedriskassesments:
         targetEntity: **SelectedRiskAssesment**
         joinTable:
           name: step2_selected_risk_assesments
           joinColumns:
             step2_id:
               referencedColumnName: id
           inverseJoinColumns:
             selected_risk_id:
               referencedColumnName: id 
         cascade: ["persist","remove"]
..............................

SelectedRiskAssesment 实体具有:

 manyToOne:
        risk_assesment:
            targetEntity: RiskAssestment
            inversedBy: selectedriskassesment
            joinColumn:
                name: risk_assesment
                referencedColumnName: id
            orphanRemoval: true

这是调查的一部分,我想删除Step2 实体并且 selectedriskassesments 但是我得到了:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`phsafety`.`step2_selected_risk_assesments`, CONSTRAINT `FK_FAFC9298EA71C84F` FOREIGN KEY (`selected_risk_id`) REFERENCES `selectedriskassesment` (`id`))

我尝试使用 orphanRemoval:true 取消绑定 RiskAssestment 实体...任何帮助都很棒

1 个答案:

答案 0 :(得分:0)

我不得不改变Step2实体:

  manyToMany:
       selectedriskassesments:
         targetEntity: **SelectedRiskAssesment**
         joinTable:
           name: step2_selected_risk_assesments
           joinColumns:
             step2_id:
               referencedColumnName: id
           inverseJoinColumns:
             selected_risk_id:
               referencedColumnName: id 
               onDelete: CASCADE
         cascade: ["persist","remove"]

必须添加 onDelete:CASCADE ,灵感来自

  • Elnur Abdurrakhimov