首先,抱歉我的英语不好......
我有四个实体:User,Application,Bundle&实体。以下是他们的关系(级联持续和删除,请参阅下面的代码):
工作正常。但是用户可以将他的两个实体作为默认实体,我需要直接访问它们。
所以我添加用户两个字段,entity1& entity2, 1-1 关系。现在我的应用程序崩溃了:
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`))
我尝试了几件事,包括那些在this post中创建的内容,但我无法修复它。
欢迎任何帮助,提前谢谢。
编辑:我需要指出用户 - >实体关系是可选的:用户的实体1& entity2可以为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;
答案 0 :(得分:33)
所以,感谢this French forum,我解决了这个问题。
我需要添加 nullable = true & @ strong \ JoinColumn
中的 onDelete =“SET 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", nullable=true, onDelete="SET NULL")
*/
private $entity1;
/**
* @ORM\OneToOne(targetEntity="\sfCommands\ContentBundle\Entity\Entity")
* @ORM\JoinColumn(name="entity2_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
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", nullable=true, onDelete="SET NULL")
*/
protected $user;
#Bundle.
/**
* @ORM\ManyToOne(targetEntity="\sfCommands\ContentBundle\Entity\Application", inversedBy="bundles", cascade={"persist"})
* @ORM\JoinColumn(name="application_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
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", nullable=true, onDelete="SET NULL")
*/
protected $bundle;
答案 1 :(得分:21)
使用onDelete =" CASCADE"如果您正在使用注释
/**
* @ORM\ManyToOne(targetEntity="Report", inversedBy="responses")
* @ORM\JoinColumn(name="reportId", referencedColumnName="id",onDelete="CASCADE")
*/
如果您使用的是yml
,请使用onDelete:CASCADEjoinColumn:
name: pid
referencedColumnName: id
onDelete: CASCADE
答案 2 :(得分:3)
onDelete =“CASCADE”也可以。但是不要忘记在DB级别更改生效之前运行app / console doctrine:schema:update --force。
答案 3 :(得分:1)
orphanRemoval有时候不起作用,因为它取决于(被调整)PersistencCollection
。我们可能会打电话给ArrayCollection#removeElement()
。
以下是PersistencCollection#remove()
if ($this->association !== null &&
$this->association['type'] & ClassMetadata::TO_MANY &&
$this->owner &&
$this->association['orphanRemoval']) {
$this->em->getUnitOfWork()->scheduleOrphanRemoval($removed);
}
和ArrayCollection不这样做。