【问题标题】:Sylius - Product Builder not using expected Entity (override)Sylius - 产品生成器不使用预期的实体(覆盖)
【发布时间】:2014-03-14 10:36:30
【问题描述】:

我正在尝试使用产品构建器创建 Sylius 产品。 当我尝试构建产品时,出现以下错误:

Found entity of type Sylius\Bundle\CoreBundle\Model\Variant on association
Shopfish\Bundle\CoreBundle\Entity\Product#variants, but expecting
Shopfish\Bundle\CoreBundle\Entity\Variant

我正在尝试使用以下代码 sn-p 构建产品:

/**
 * @Route("/admin/products/create", name="sf_product_create")
 * @Template()
 */
public function createAction(Request $request)
{
    $product = $this->get('sylius.builder.product')
        ->create('My Lovely Product')
        ->setPrice(18790)
        ->setDescription('Such a lovely product.')
        ->addProperty('color', 'Red')
        ->save()
    ;

    return array('product' => $product);
}

我已经像这样覆盖了SyliusProduct

use Sylius\Bundle\CoreBundle\Model\Product as BaseProduct;

class Product extends BaseProduct
{
    protected $resource; // going to do something with this later on.
}

SyliusVariant 已以类似方式被覆盖:

use Sylius\Bundle\CoreBundle\Model\Variant as BaseVariant;

class Variant extends BaseVariant {
    protected $salePrice;

    // ... + getSalePrice() & setSalePrice()
}

我已更新 sylius.yml 中的配置文件以使用我自己的类:

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

sylius_variable_product:
    classes:
        variant:
            model: Shopfish\Bundle\CoreBundle\Entity\Variant
            repository: Sylius\Bundle\CoreBundle\Repository\VariantRepository
            form: Sylius\Bundle\CoreBundle\Form\Type\VariantType

为什么它使用Sylius 变体进行关联,而它正确地期望我自己的Variant 实例?

编辑

这是我的产品映射:

<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="Shopfish\Bundle\CoreBundle\Entity\Product" table="sylius_product">
        <one-to-many field="variants" target-entity="Shopfish\Bundle\CoreBundle\Entity\Variant" mapped-by="product">
            <cascade>
                <cascade-all />
            </cascade>
        </one-to-many>
    </entity>

</doctrine-mapping>

【问题讨论】:

  • 你能发布Shopfish\Bundle\CoreBundle\Entity\Product的映射数据吗?
  • 我已经为产品尝试了一些自定义映射,但它似乎无法覆盖 variants 属性。我已将其添加到主帖中。

标签: symfony sylius


【解决方案1】:

我认为这是一个小错误,但您可以通过覆盖您自己的 Product 类中的构造函数来轻松解决它。

CoreBundle\Model\Product:

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

    $this->setMasterVariant(new Variant());
    $this->taxons = new ArrayCollection();

    $this->variantSelectionMethod = self::VARIANT_SELECTION_CHOICE;
}

Master Variant 设置为 CoreBundle\Model\Variant 的一个实例。 覆盖您自己的 Product 模型中的构造函数:

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

    $this->setMasterVariant(new YourCustomVariant());
}

这应该可以修复了 :)。

【讨论】:

  • 您好 Ruud,感谢您的回复。这确实解决了unexpected class 异常,但是这种方法导致价格设置不正确:Integrity constraint violation: 1048 Column 'price' cannot be null,即使我在产品生成器上调用了setPrice()。有什么线索吗?
  • 有趣的是$product-&gt;getPrice() 确实返回18790,而不调用save()。有没有其他变种可能会毁了一切?
  • 我刚刚更新了我的答案,因为复制整个构造函数不是很干净。不过,价格问题应该与此覆盖无关。
  • 奇怪的是,当sylius_product.classes.model 设置为Sylius\Bundle\CoreBundle\Model\Product 时,我的产品会正确保存,但设置为Shopfish\Bundle\CoreBundle\Entity\Product 时会出现上述错误..
  • 价格问题可能是由于与变体的某些重复关联造成的。您正在重新定义现有关联。尝试删除它(由于产品映射为空,您甚至可以完全删除映射,但我不知道 Doctrine 是否接受)
【解决方案2】:

要遵循最新的 sylius 开发,您需要扩展 Products 模型并将 __construct 替换为以下代码

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

    $this
        ->setVariants(new ArrayCollection())
        ->setMasterVariant(new YourCustomVariant())
    ;
}

【讨论】:

  • 感谢您的宝贵时间,但很抱歉您的回答只是一个代码-sn-p,它不是不言自明的,可以使用一些解释。除此之外,答案已经被接受。不过谢谢:-)。
  • 当然,添加一些解释行。也有小的区别,在设置主之前清除变体很重要
  • 感谢您的澄清,现在更有意义了!
猜你喜欢
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-05
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
相关资源
最近更新 更多