[英]Sylius override product Integrity constraint violation
我正在嘗試覆蓋產品捆綁包,但成功完成,但是在嘗試刪除將產品作為子產品的父實體時遇到錯誤
這是映射文件product.orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping">
<mapped-superclass name="Sylius\Component\Product\Model\Product" table="sylius_product">
<id name="id" column="id" type="integer">
<generator strategy="AUTO" />
</id>
<field name="name" column="name" type="string">
<gedmo:versioned />
</field>
...
</mapped-superclass>
</doctrine-mapping>
這是我的product.orm.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="WeShop\Bundle\CoreBundle\Entity\Product" table="sylius_product">
<many-to-one field="boutique" target-entity="WeShop\Bundle\BoutiqueBundle\Entity\Boutique" inversed-by="produits">
<join-column name="boutique_id" referenced-column-name="id" nullable="true" onDelete="SET NULL" />
</many-to-one>
</entity>
</doctrine-mapping>
在WeShop \\ Bundle \\ BoutiqueBundle \\ Entity \\ Boutique中,我確實有
<?php
namespace WeShop\Bundle\BoutiqueBundle\Entity;
use Sylius\Component\Core\Model\ProductInterface as ProductInterface;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
class Boutique
{
private $produits;
public function __construct(){
$this->produits = new ArrayCollection();
}
public function getProduits(){
return $this->produits;
}
public function setProduits(ProductInterface $produit){
$this->produits[] = $produit;
return $this;
}
}
在WeShop \\ Bundle \\ CoreBundle \\ Entity \\ Product中,我確實有這個
<?php
namespace WeShop\Bundle\CoreBundle\Entity;
use WeShop\Bundle\CoreBundle\Model;
use WeShop\Bundle\BoutiqueBundle\Entity\BoutiqueInterface as BoutiqueInterface;
use Sylius\Component\Core\Model\Product as BaseProduct;
class Product extends BaseProduct
{
private $boutique;
public function getBoutique()
{
return $this->boutique;
}
public function setBoutique(BoutiqueInterface $boutique = null)
{
$this->boutique = $boutique;
return $this;
}
}
與精品實體(商店)合作,我可以按照我希望一切正常的方式進行操作,現在的問題是當我嘗試刪除它時
An exception occurred while executing 'DELETE FROM weshop_boutique WHERE id = ?' with params [2]:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`sylius_dev`.`sylius_product`, CONSTRAINT `FK_677B9B74AB677BE6` FOREIGN KEY (`boutique_id`) REFERENCES `weshop_boutique` (`id`))
請注意,一家精品店(商店)可以有多個產品,但是一個產品只能是一個精品店(商店)的子產品
我確實有一個具有相同邏輯的文檔包,並且當我嘗試刪除精品店時(知道表sylius_product中沒有引用精品店{null}的行),文檔被刪除了
這是我的文檔orm xml文件
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity table="weshop_documents" repository-class="WeShop\Bundle\DocumentsBundle\Entity\DocumentsRepository" name="WeShop\Bundle\DocumentsBundle\Entity\Documents">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="intitule" type="string" column="intitule" length="255"/>
<field name="url" type="string" column="url" length="255"/>
<many-to-one field="boutique" target-entity="WeShop\Bundle\BoutiqueBundle\Entity\Boutique" inversed-by="documents">
<join-column name="boutique_id" referenced-column-name="id" nullable="false" />
</many-to-one>
</entity>
</doctrine-mapping>
先感謝您
Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`sylius_dev`.`sylius_product`, CONSTRAINT `FK_677B9B74AB677BE6` FOREIGN KEY (`boutique_id`) REFERENCES `weshop_boutique` (`id`))
如果我理解現在,這是針對此錯誤的解決方案。
您在sylius_product
<=> weshop_boutique
之間有關系。 因此,當您創建產品時, weshop_boutique
id
weshop_boutique
到sylius_product
表。
所以,當你想刪除的東西weship_boutique
這與關系sylius_product
那么首先你要刪除產品sylius_product
這與關系weship_boutique
后,它會正常工作。
例如
=> you have one field in "weship_boutique" table where "id = 1". => you have one field in "sylius_product" table where "boutique_id = 1". => so when you would like to delete field from "weship_boutique" table where "id = 1" at the same time "sylius_product" need it because you have foreign key relation between (boutique_id) of table sylius_product and (id) of table "weship_boutique".
因此,首先從sylius_product中刪除產品(標識為weship_boutique),然后嘗試從weship_boutique中刪除字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.