问题描述
过去几天,我一直在努力解决使用Doctrine的一个简单的级联去除案例。
学说和Symfony是最新的。
我有两个实体意图和资产通过两个关系OneToOne和OneToMany相互链接。
I've been struggling for the past few days on a simple case of cascade removing using Doctrine.Doctrine and Symfony are up to date.I have two entities Serie and Asset that are linked to each other by two relationships OneToOne and OneToMany.
模式完全是这样的:
意甲有很多资产。 (内容)。
A Serie has many Assets. (content).
意甲可以拥有资产。 (一个预览,此字段为空)
A Serie can have an Asset. (a preview, this field is nullable).
但是,无论我如何尝试写入和重写注释,我一直都会遇到这个错误:
However, no matter how I try to write and rewrite the annotations, I ALWAYS end up with this error:
SQLSTATE [23000]:完整性约束违反:1451无法删除或
更新父行:外键约束失败
( galanthis
。资产
,CONSTRAINT FK_2AF5A5CAA3A9334
FOREIGN KEY
( serie
)参考 serie
( id
))
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (galanthis
.asset
, CONSTRAINT FK_2AF5A5CAA3A9334
FOREIGN KEY (serie
) REFERENCES serie
(id
))
当然,如果我在以下代码中删除预览字段及其注释,则问题将消失:
Of course, the problem disappear if I delete the "preview" field and its annotations in the following code:
/**
* Serie
*
* @ORM\Table(name="serie")
* @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
* @ORM\HasLifecycleCallbacks
*/
class Serie
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=96)
*/
private $title;
/**
* @var integer
*
* @Gedmo\SortablePosition
* @ORM\Column(name="position", type="integer", nullable=true)
*/
private $position;
/**
* @var \Portfolio
*
* @ORM\ManyToOne(targetEntity="Portfolio", inversedBy="series")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="portfolio", referencedColumnName="id")
* })
*/
private $portfolio;
/**
* @var \Asset
*
* @ORM\OneToOne(targetEntity="Asset")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="preview", referencedColumnName="id", nullable=true, onDelete="SET NULL")
* })
*/
private $preview;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Asset", mappedBy="serie", cascade={"remove"})
**/
private $assets;
以下是资产实体的代码:
Here's the code for the Asset entity:
/**
* Asset
*
* @ORM\Table(name="asset")
* @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
* @ORM\HasLifecycleCallbacks
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="asset", type="string")
* @ORM\DiscriminatorMap({"asset" = "Asset", "video" = "Video","image" = "Image"})
*
*/
class Asset
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=128)
*/
protected $path;
/**
* @var string
*
* @ORM\Column(name="filename", type="string", length=64)
*/
protected $filename;
/**
* @var integer
*
* @ORM\Column(name="position", type="integer", nullable=true)
* @Gedmo\SortablePosition
*/
protected $position;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=255, nullable=true)
*/
protected $description;
/**
* @var string
*
* @ORM\Column(name="mime", type="string", length=16, nullable=true)
*/
protected $mime;
/**
* @var \Serie
*
* @ORM\ManyToOne(targetEntity="Serie", inversedBy="assets")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="serie", referencedColumnName="id")
* })
*/
protected $serie;
/**
* @var UploadedFile
*/
protected $file;
/**
* @var string
*/
protected $extension;
这让我疯狂,这只是一些简单的关系...有一个错误我不是看到了,还是需要使用解决方法?
It's driving me crazy, it's just some simple relationships... Is there a mistake I'm not seeing anymore, or do i need to use a workaround?
推荐答案
我的猜测是设置cascade = {remove}在资产实体中的 ManyToOne关系而不是其他方式。这样,它告诉Doctrine当你删除与许多资产相关联的系列时该怎么办。
My guess is to set the cascade={"remove"} on the ManyToOne relationship in the Asset entity and not the other way around. That way, it tells Doctrine what to do when you delete a serie that is linked to many assets.
这篇关于OneToMany和OneToOne中的教义级联删除失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!