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.
The schema is exactly like this :
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:
An exception occurred while executing 'DELETE FROM serie WHERE id = ?' with params [1]:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (
galanthis
.asset
, CONSTRAINTFK_2AF5A5CAA3A9334
FOREIGN KEY (serie
) REFERENCESserie
(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?
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.