Override CoreBundle/Model/Product

烂漫一生 提交于 2020-01-17 01:15:40

问题


I'm struggling to override the Product an I'm not sure if it is a bug or not. The overriding is working correctly but when I'm going to create a new Product in the Sylius Backend I'm getting following Exception:

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

My Code looks like this:

<?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

Has anyone an idea if I'm missing something?

Thanks, David


回答1:


Just replace your product model's construct() with below code. After it will works correctly.

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;
   }
}


来源:https://stackoverflow.com/questions/22919004/override-corebundle-model-product

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!