【问题标题】:Override CoreBundle/Model/Product覆盖 CoreBundle/模型/产品
【发布时间】:2014-04-07 17:20:12
【问题描述】:

我正在努力覆盖产品,我不确定它是否是错误。 覆盖工作正常,但是当我要在 Sylius 后端创建新产品时,出现以下异常:

An exception occurred while executing 'INSERT INTO sylius_variant 
(is_master,     presentation, available_on, created_at, updated_at, deleted_at, 
 sku, price, on_hold, on_hand, available_on_demand, width, height, depth, weight,
 product_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' 

with params [1, null, "2014-04-07 18:43:00", "2014-04-07 19:12:25", 
"2014-04-07 19:12:25", null, null, 10000, 0, 0, 1, null, null, null, null, null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null

我的代码如下所示:

<?php


namespace Acme\ShopBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Sylius\Bundle\CoreBundle\Model\Product as BaseProduct;
use Doctrine\Common\Collections\ArrayCollection;
use Sylius\Bundle\CoreBundle\Model\Variant;

/**
 * @ORM\Entity()
 * @ORM\Table(name="sylius_product")
 */
class Product extends BaseProduct
{

    public function __construct() {
        parent::__construct();
    }
}

sylius.yml:

sylius_product:
    classes:
        product:
            model: Acme\ShopBundle\Entity\Product
            controller: Sylius\Bundle\CoreBundle\Controller\ProductController
            repository: Sylius\Bundle\CoreBundle\Repository\ProductRepository
            form: Acme\ShopBundle\Form\Type\ProductType

如果我遗漏了什么,有人知道吗?

谢谢, 大卫

【问题讨论】:

    标签: sylius


    【解决方案1】:

    只需用以下代码替换您的产品模型的construct()。之后就可以正常工作了。

    use Doctrine\Common\Collections\ArrayCollection;
    
    class Product extends BaseProduct
    {
    
       public function __construct() {
          $this->availableOn = new \DateTime();
          $this->properties = new ArrayCollection();
          $this->createdAt = new \DateTime();
    
          $this->variants = new ArrayCollection();
          $this->options = new ArrayCollection();
    
          $this->setMasterVariant(new Variant());
          $this->taxons = new ArrayCollection();
          $this->variantSelectionMethod = self::VARIANT_SELECTION_CHOICE;
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 2019-09-06
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多