First, sorry for my poor English...
I got four entities : User, Application, Bundle & Entity. Here are their relations (with cascading persist & remove, see code below) :
- User 1-n Application
- Application 1-n Bundle
- Bundle 1-n Entity
It's working fine. But an User can have two of his entities as default, and I need to access them directly.
So I add on User two fields, entity1 & entity2, with a 1-1 relation. And now my app crashes :
An exception occurred while executing 'DELETE FROM bundle WHERE id = ?' with params {"1":13}:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`misc`.`entity`, CONSTRAINT `FK_E284468F1FAD9D3` FOREIGN KEY (`bundle_id`) REFERENCES `bundle` (`id`))
I tried several things, including those founded in this post, but I wasn't able to fix it.
Any help be welcome, thanks in advance.
EDIT : I need to point out that User->Entity relations are optionnal : User's entity1 & entity2 can be null. The error happens even if they are null both.
Here are my entities definitions :
# User :
/**
* @ORMOneToMany(targetEntity="sfCommandsContentBundleEntityApplication", mappedBy="user", cascade={"remove"}, orphanRemoval=true)
* @ORMOrderBy({"name" = "ASC"})
*/
protected $applications;
/**
* @ORMOneToOne(targetEntity="sfCommandsContentBundleEntityEntity")
* @ORMJoinColumn(name="entity1_id", referencedColumnName="id")
*/
private $entity1;
/**
* @ORMOneToOne(targetEntity="sfCommandsContentBundleEntityEntity")
* @ORMJoinColumn(name="entity2_id", referencedColumnName="id")
*/
private $entity2;
#Application :
/**
* @ORMOneToMany(targetEntity="sfCommandsContentBundleEntityBundle", mappedBy="application", cascade={"remove"}, orphanRemoval=true)
* @ORMOrderBy({"name" = "ASC"})
*/
protected $bundles;
/**
* @ORMManyToOne(targetEntity="sfCommandsUserBundleEntityUser", inversedBy="applications", cascade={"persist"})
* @ORMJoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
#Bundle :
/**
* @ORMManyToOne(targetEntity="sfCommandsContentBundleEntityApplication", inversedBy="bundles", cascade={"persist"})
* @ORMJoinColumn(name="application_id", referencedColumnName="id")
*/
protected $application;
/**
* @ORMOneToMany(targetEntity="sfCommandsContentBundleEntityEntity", mappedBy="bundle", cascade={"remove"}, orphanRemoval=true)
* @ORMOrderBy({"name" = "ASC"})
*/
protected $entitys;
#Entity :
/**
* @ORMManyToOne(targetEntity="sfCommandsContentBundleEntityBundle", inversedBy="entitys", cascade={"persist"})
* @ORMJoinColumn(name="bundle_id", referencedColumnName="id")
*/
protected $bundle;
See Question&Answers more detail:
os