まず、英語が下手でごめんなさい...
ユーザー、アプリケーション、バンドル、エンティティの4つのエンティティを取得しました。それらの関係は次のとおりです(カスケードのpersist&removeを使用します。以下のコードを参照してください):
- ユーザー1-nアプリケーション
- アプリケーション1-nバンドル
- バンドル1-nエンティティ
正常に動作しています。しかし、ユーザーはデフォルトで2つのエンティティを持つことができ、私はそれらに直接アクセスする必要があります。
そこで、ユーザーに1対1の関係を持つentity1とentity2の2つのフィールドを追加します。そして今、私のアプリはクラッシュします:
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`))
この投稿で作成されたものを含め、いくつかのことを試しましたが、修正できませんでした。
よろしくお願いします。
編集:ユーザー->エンティティの関係はオプションであることを指摘する必要があります:ユーザーのentity1とentity2はnullになる可能性があります。両方がnullの場合でも、エラーが発生します。
これが私のエンティティの定義です:
# User :
/**
* @ORM\OneToMany(targetEntity="\sfCommands\ContentBundle\Entity\Application", mappedBy="user", cascade={"remove"}, orphanRemoval=true)
* @ORM\OrderBy({"name" = "ASC"})
*/
protected $applications;
/**
* @ORM\OneToOne(targetEntity="\sfCommands\ContentBundle\Entity\Entity")
* @ORM\JoinColumn(name="entity1_id", referencedColumnName="id")
*/
private $entity1;
/**
* @ORM\OneToOne(targetEntity="\sfCommands\ContentBundle\Entity\Entity")
* @ORM\JoinColumn(name="entity2_id", referencedColumnName="id")
*/
private $entity2;
#Application :
/**
* @ORM\OneToMany(targetEntity="\sfCommands\ContentBundle\Entity\Bundle", mappedBy="application", cascade={"remove"}, orphanRemoval=true)
* @ORM\OrderBy({"name" = "ASC"})
*/
protected $bundles;
/**
* @ORM\ManyToOne(targetEntity="\sfCommands\UserBundle\Entity\User", inversedBy="applications", cascade={"persist"})
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
#Bundle :
/**
* @ORM\ManyToOne(targetEntity="\sfCommands\ContentBundle\Entity\Application", inversedBy="bundles", cascade={"persist"})
* @ORM\JoinColumn(name="application_id", referencedColumnName="id")
*/
protected $application;
/**
* @ORM\OneToMany(targetEntity="\sfCommands\ContentBundle\Entity\Entity", mappedBy="bundle", cascade={"remove"}, orphanRemoval=true)
* @ORM\OrderBy({"name" = "ASC"})
*/
protected $entitys;
#Entity :
/**
* @ORM\ManyToOne(targetEntity="\sfCommands\ContentBundle\Entity\Bundle", inversedBy="entitys", cascade={"persist"})
* @ORM\JoinColumn(name="bundle_id", referencedColumnName="id")
*/
protected $bundle;