In my model I have
Terrain.php
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class Terrain
{
/**
* @var lots
*
* @ORM\OneToMany(targetEntity="Lot", mappedBy="terrain")
*/
private $lotes;
/**
* @ORM\PreRemove
*/
public function deleteAllLots()
{
$lots = $this->getLots();
foreach ($lots as $lot) {
$this->lots->removeElement($lot);
}
}
}
Lot.php
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class Lot
{
/**
* @var Terrain
*
* @ORM\ManyToOne(targetEntity="Terrain", inversedBy="lots")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="terrain_id", referencedColumnName="id")
* })
*/
var $terrain
}
Lot
I’m trying to delete a terrain, but formerly i deleted all lots associated with it. when i try to delete a Terrain, i get an error SQLSTATE[23000]: Integrity constraint violation
You still have a reference from
lotstoterrainon deleting, that’s why you have anIntegrity constrait violation.Try this :
If you want to delete
lotsfrom database, you should usecascadeproperty ofOneToManyrelation (One-To-Many reference on doctrine-project.com), like this :On
Terrainremove, all lots will be removed.