2014-04-27 6 views
0

Я пытаюсь переопределить пакет продуктов, я сделал это успешно, но получает сообщение об ошибке при попытке удалить родительский объект, что есть продукты, какSylius переопределение продукт нарушение целостности ограничения

порожденных

вот отображение файл 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}), документы удаляются

вот мои документы ОРМ 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> 

Спасибо заранее

ответ

0

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`)) , если я понял currectly, то это решение для этой ошибки.

  1. у вас есть отношения между sylius_product < =>weshop_boutique. поэтому, когда вы создаете продукт, вы передаете id из weshop_boutique в таблицу 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". 
    

Так первый Удалить продукт (с идентификатором weship_boutique) из sylius_product затем попытаться удалить поле из weship_boutique.

Смежные вопросы