I'll be specific. I have entities quest, city and organizer. When i try to remove organizer, that has relationship with quest error appears
An exception occurred while executing 'DELETE FROM Organizer WHERE id = ?' with params [522]:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (
test.Quest, CONSTRAINTFK_82D6D713876C4DDAFOREIGN KEY (organizer_id) REFERENCESOrganizer(id))
I am absolutely confused with right way of configuring relationships. The logic is:
- We add new cities.
- We add new organizers and put city into organizer.
- We create new quest and than put city and organizer into it.
- If we remove organizer - organizers id in quests must be removed(quest must not be removed).
- If we remove organizer - and organizers id in cities must be removed too (and city must not be removed).
I am sorry for my very dumb description, but i trying to exclude misunderstandings.
Well, what is right entity relationship should i use for my situation?
class Quest { ... /** * @ORM\JoinColumn(name="organizer_id", referencedColumnName="id") * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Organizer") */ protected $organizer; } class Organizer { ... /** * @ORM\Column(type="string", length=140) */ protected $name; /** * @ORM\ManyToMany(targetEntity="AppBundle\Entity\City", inversedBy="organizers") * @ORM\JoinTable(name="organizers_cities") */ protected $cities; public function __construct() { $this->quests = new ArrayCollection(); // i don't remember why this is here } } class City { /** * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Organizer", mappedBy="cities") */ protected $organizers; /** * Constructor */ public function __construct() { $this->quests = new \Doctrine\Common\Collections\ArrayCollection(); $this->organizers = new \Doctrine\Common\Collections\ArrayCollection(); } }