我正在尝试使用产品构建器创建 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>