Question

I'm trying to create a Sylius product with the product builder. When I try to build a product I get the following error:

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

I'm trying to build a product with the below code snippet:

/**
 * @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);
}

I've overridden the SyliusProduct as such:

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

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

The SyliusVariant has been overriden in a similar fashion:

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

class Variant extends BaseVariant {
    protected $salePrice;

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

I've updated the config file in sylius.yml to use my own classes:

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

Why is it using the Sylius variant for its association while it properly expects an instance of my own Variant?

Edit

Here's my Product mapping:

<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>
Was it helpful?

Solution

I think this is a little bug, but you can easily work around it by overriding the constructor in your own Product class.

CoreBundle\Model\Product:

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

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

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

The Master Variant is set to an instance of CoreBundle\Model\Variant. Override the constructor in your own Product model:

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

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

This should probably be fixed though :).

OTHER TIPS

To follow latest sylius dev you need to extend Products model and also replace __construct with the below code

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

    $this
        ->setVariants(new ArrayCollection())
        ->setMasterVariant(new YourCustomVariant())
    ;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top