gpt4 book ai didi

php - 删除带有 FK 的行作为 PK + Symfony2 和 Doctrine

转载 作者:行者123 更新时间:2023-11-30 00:41:37 26 4
gpt4 key购买 nike

我有一个表 items ,其中包含 item_id, item_title, ... 。我还有一个表 articles,其中包含 PK item_id(它是表 items 中的 FK)和一个字段 article_body 。我还具有从 itemscategories 的多对多关系 (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)

我做错了什么?

最佳答案

答案正如哈斯特所说,我没有在我的文章表中的 FK item_id 处定义“CASCADE ON DELETE”。

关于php - 删除带有 FK 的行作为 PK + Symfony2 和 Doctrine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21730453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com