我正在尝试使用产品构建器创建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
我正在尝试使用以下代码段构建产品:
/**
* @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>
答案 0 :(得分:2)
我认为这是一个小错误,但您可以通过覆盖自己的Product类中的构造函数轻松解决它。
CoreBundle \型号\产品:
public function __construct()
{
parent::__construct();
$this->setMasterVariant(new Variant());
$this->taxons = new ArrayCollection();
$this->variantSelectionMethod = self::VARIANT_SELECTION_CHOICE;
}
Master Variant设置为CoreBundle \ Model \ Variant的实例。 在您自己的产品模型中覆盖构造函数:
public function __construct()
{
parent::__construct();
$this->setMasterVariant(new YourCustomVariant());
}
这可能应该修复:)。
答案 1 :(得分:2)
要关注最新的sylius dev,您需要扩展Products模型,并将__construct替换为以下代码
public function __construct()
{
parent::__construct();
$this
->setVariants(new ArrayCollection())
->setMasterVariant(new YourCustomVariant())
;
}