Doctrine: Exception in One-To-Many cascading: Integrity constraint violation 1451

I am trying to build a very basic CMS using Symfony and Doctrine. I have entities emulating my sites’ structure like so:

Entity: Page

/**
 * @ORMEntity(repositoryClass="AppRepositoryContentTreePageRepository")
 */
class Page extends SortableBase
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     * @ORMOrderBy({"sort_order" = "ASC"})
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity="Website", inversedBy="pages", cascade={"all"}, fetch="EAGER")
     */
    private $website;

    /**
     * Owning side of relation
     * @ORMOneToMany(targetEntity="Section", mappedBy="page", fetch="EAGER")
     */
    private $sections;


    public function __construct()
    {
    ...

Entity: Section

/**
 * @ORMEntity(repositoryClass="AppRepositoryContentTreeSectionRepository")
 */
class Section extends SortableBase
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity="Page", inversedBy="sections", cascade={"all"}, fetch="EAGER")
     */
    private $page;

    /**
     * Owning side of relation
     * @ORMOneToMany(targetEntity="Entry", mappedBy="section", fetch="EAGER")
     */
    private $entries;

    /**
     * @ORMManyToOne(targetEntity="AppEntitySectionTemplatingSectionType", fetch="EAGER")
     */
    private $sectionType;


    public function __construct()
    {

Entity: Entry

/**
 * @ORMEntity(repositoryClass="AppRepositoryContentTreeEntryRepository")
 */
class Entry extends SortableBase
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity="Section", inversedBy="entries", cascade={"all"}, fetch="EAGER")
     */
    private $section;

    /**
     * Owning side of relation
     * @ORMOneToMany(targetEntity="AppEntityContentTypesContent", mappedBy="entry", fetch="EAGER")
     */
    private $contents;


    public function __construct()
    {
    ...

So in the end a Page can have Sections which can have Entries and so forth. Now, from what I gathered from the docs I was under the assumption that with how I setup the cascades I could just go and use the EntityManager to remove an instance of any Entity (lets say Section) and it would automatically delete that instance as well as all contained Entries.

However, when I do something like:

$section = $this->getSectionByID($sectionid);

$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($section);
$entityManager->flush();

as soon as the Section has any Entries, I get:

An exception occurred while executing 'DELETE FROM section WHERE id = ?' with params [10]:

SQLSTATE[23000]: Integrity constraint violation: 
1451 Cannot delete or update a parent row: 
a foreign key constraint fails (`webdata`.`entry`, CONSTRAINT `FK_2B219D70D823E37A` 
FOREIGN KEY (`section_id`) REFERENCES `section` (`id`))

I know what it it means but I just cannot figure out what I should do different here in order to force the EntityManager to traverse down my entity graph and delete everything from bottom to top.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *