-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Open
Labels
Description
[Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException]
An exception occurred while executing 'DELETE FROM subitem WHERE id = ?' wi
th params [4]:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or upda
te a parent row: a foreign key constraint fails (`shop`.`item`, CONSTRAINT
`FK_1F1B251EB3AE4213` FOREIGN KEY (`featured_item_id`) REFERENCES `subitem`
(`id`))
is caused by
$item = new Item();
$sub1 = new Subitem();
$sub2 = new Subitem();
$item->addItem($sub1);
$item->addItem($sub2);
$item->setFeaturedItem($sub2);
$em->persist($item);
$em->flush();
$em->remove($item);
$em->flush();Entity mappings:
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Item
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Subitem", mappedBy="item", cascade={"all"}, orphanRemoval=true)
*/
protected $items;
/**
* @var Subitem
*
* @ORM\ManyToOne(targetEntity="Subitem")
*/
protected $featuredItem;
public function __construct()
{
$this->items = new ArrayCollection();
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return ArrayCollection
*/
public function getItems()
{
return $this->items;
}
public function addItem(Subitem $item)
{
$this->items[] = $item;
$item->setItem($this);
}
public function removeItem(Subitem $item = null) {
if ($this->featuredItem === $item) {
$this->featuredItem = null;
}
$this->items->removeElement($item);
}
/**
* @return Subitem
*/
public function getFeaturedItem()
{
return $this->featuredItem;
}
/**
* @param Subitem $featuredItem
*/
public function setFeaturedItem(Subitem $featuredItem)
{
$this->featuredItem = $featuredItem;
}
}
/**
* @ORM\Entity
*/
class Subitem
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var Item
*
* @ORM\ManyToOne(targetEntity="Item", inversedBy="items")
*/
protected $item;
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @return Item
*/
public function getItem()
{
return $this->item;
}
/**
* @param Item $item
*/
public function setItem($item)
{
$this->item = $item;
}
}Reactions are currently unavailable