我有items
表item_id, item_title, ...
。我还有一张表articles
,其中PK item_id
是表items
中的FK和字段article_body
。我也有items
到categories
(item_has_catogories
)之间的多对多关系。
这是我的文章实体:
<?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;
}
}
正如您所看到的,我的文章实体扩展了项目实体,我的项目实体:
<?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;
}
}
现在我想删除一篇文章(=项目)。 这就是我的工作:
$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();
但我总是得到这个错误:
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)
我做错了什么?
答案 0 :(得分:0)
答案是因为Hast说我没有在我的文章表格中的FK item_id中定义'CASCADE ON DELETE'。