【问题标题】:Sylius override product Integrity constraint violationSylius 覆盖产品完整性约束违规
【发布时间】:2014-04-27 20:08:52
【问题描述】:

我正在尝试覆盖产品捆绑包,我成功地做到了,但在尝试删除将产品作为子项的父实体时出错

这是映射文件 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>

提前谢谢你

【问题讨论】:

    标签: php xml symfony doctrine-orm sylius


    【解决方案1】:

    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`)) 如果我理解正确,那么这是解决此错误的方法。

    1. 你和sylius_product有关系weshop_boutique。因此,当您创建产品时,您会将 weshop_boutique 中的 id 传递给 sylius_product 表。

    2. 因此,当您想从weship_boutique 中删除与sylius_product 相关的内容时,首先您必须从sylius_product 中删除与weship_boutique 相关的产品,然后才能正常工作。

    3. 例如

        =>   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 中删除产品(id 为 weship_boutique),然后尝试从 weship_boutique 中删除字段。

    【讨论】:

      猜你喜欢
      • 2012-10-18
      • 2013-07-04
      • 2020-04-22
      • 1970-01-01
      • 2015-05-07
      • 2016-11-22
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多