0

I have a table items with item_id, item_title, ... . I also have a table articles with a PK item_id that is a FK from the table items and a field article_body. I also have a many to many relationship from items to categories (item_has_catogories).

This is my Article Entity:

<?php

namespace Dxsolutions\DxsolutionsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Item
 *
 * @ORM\Table(name="articles")
 * @ORM\Entity
 */
class Article extends Item
{
    /**
     * @var string
     *
     * @ORM\Column(name="article_body", type="text")
     */
    protected $body;

    /**
     * Set body
     *
     * @param string $body
     * @return Article
     */
    public function setBody($body)
    {
        $this->body = $body;

        return $this;
    }

    /**
     * Get body
     *
     * @return string
     */
    public function getBody()
    {
        return $this->body;
    }
}

As you can see my Article Entity extends from the Item Entity, my Item Entity:

<?php

namespace Dxsolutions\DxsolutionsBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Item
 *
 * @ORM\Table(name="items")
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="item_type", type="string")
 * @ORM\DiscriminatorMap({"ITEM"="Item", "ARTICLE"="Article"})
 */
class Item
{
    /**
     * @var integer
     *
     * @ORM\Column(name="item_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="item_title", type="string", length=255)
     */
    private $title;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="item_created", type="datetimetz")
     */
    private $created;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="item_modified", type="datetimetz")
     */
    private $updated;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="item_deleted", type="datetimetz")
     */
    private $deleted;

    /**
     * @var \Doctrine\Common\Collections\ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="Category", inversedBy="items")
     * @ORM\JoinTable(name="items_has_categories",
     *      joinColumns={@ORM\JoinColumn(name="item_id", referencedColumnName="item_id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="category_id")}
     *      )
     */
    protected $categories;

    /**
     * Constructor has to create Doctrine ArrayCollections
     */
    public function __construct()
    {
        $this->categories = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set title
     *
     * @param string $title
     * @return Item
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title
     *
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set created
     *
     * @param \DateTime $created
     * @return Item
     */
    public function setCreated($created)
    {
        $this->created = $created;

        return $this;
    }

    /**
     * Get created
     *
     * @return \DateTime 
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * Set updated
     *
     * @param \DateTime $updated
     * @return Item
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

    /**
     * Get updated
     *
     * @return \DateTime
     */
    public function getUpdated()
    {
        return $this->updated;
    }

    /**
     * Set deleted
     *
     * @param \DateTime $deleted
     * @return Item
     */
    public function setDeleted($deleted)
    {
        $this->deleted = $deleted;

        return $this;
    }

    /**
     * Get deleted
     *
     * @return \DateTime 
     */
    public function getDeleted()
    {
        return $this->deleted;
    }

    /**
     * Set category
     *
     * @param Category $category
     */
    public function setCategories(Category $category)
    {
        $this->categories->add($category);
    }

    /**
     * Get categories
     *
     * @return \Doctrine\Common\Collections\ArrayCollection
     */
    public function getCategories()
    {
        return $this->categories;
    }
}

Now I would like to delete an article (= an item). This is what I do :

$em = $this->getDoctrine()->getManager();

$article =  $em->getRepository('DxSolutionsBundle:Article')->find($id);

foreach($article->getCategories() as $c) {
    $article->getCategories()->removeElement($c);
    $em->flush();
}

$em->remove($article);
$em->flush();

But I always get this error:

An exception occurred while executing 'DELETE FROM items WHERE item_id = ?' with params [4]:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`dx-solutions`.`articles`, CONSTRAINT `fk_articles_items1` FOREIGN KEY (`item_id`) REFERENCES `items` (`item_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

What am I doing wrong?

1

1 Answer 1

0

The answer was as Hast said that I didn't defined 'CASCADE ON DELETE' at my FK item_id in my article table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.