简体   繁体   中英

Error when overriding Variant class in custom Sylius bundle

I have been attempting to derive a child class of Sylius:VariableProductBundle:Variant within my own bundle, following the guide here .

I created a new bundle, AcmeVariableProductBundle which I derived from SyliusVariableProduct bundle via the method outlined here .

After following the steps outlined in the Sylius documentation for overriding models, I have the following error:

Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("The class 'Acme\\Bundle\\VariableProductBundle\\Model\\Variant' was not found in the chain configured namespaces Sylius\\Bundle\\CartBundle\\Entity, FOS\\UserBundle\\Entity, Acme\\Bundle\\CatalogueBundle\\Entity, Sylius\\Bundle\\SalesBundle\\Model, Sylius\\Bundle\\MoneyBundle\\Model, Sylius\\Bundle\\SettingsBundle\\Model, Sylius\\Bundle\\CartBundle\\Model, Sylius\\Bundle\\ProductBundle\\Model, Sylius\\Bundle\\VariableProductBundle\\Model, Sylius\\Bundle\\TaxationBundle\\Model, Sylius\\Bundle\\ShippingBundle\\Model, Sylius\\Bundle\\PaymentsBundle\\Model, Sylius\\Bundle\\PromotionsBundle\\Model, Sylius\\Bundle\\AddressingBundle\\Model, Sylius\\Bundle\\InventoryBundle\\Model, Sylius\\Bundle\\TaxonomiesBundle\\Model, Sylius\\Bundle\\CoreBundle\\Model, FOS\\UserBundle\\Model") in SyliusWebBundle:Frontend/Homepage:main.html.twig at line 2.

My class definition looks thus:

namespace Acme\Bundle\VariableProductBundle\Model;

use Doctrine\Common\Collections\ArrayCollection;
use Sylius\Bundle\VariableProductBundle\Model\Variant as BaseVariant;

class Variant extends BaseVariant
{
    ...
}

I created an xml file at: Acme/Bundle/VariableProductBundle/Resources/config/doctrine/Variant.orm.xml It looks like this:

<?xml version="1.0" encoding="UTF-8" ?>

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

    <mapped-superclass name="Acme\Bundle\VariableProductBundle\Model\Variant" table="sylius_variant">
        <field name="sku" column="sku" type="string" nullable="true" />
        <field name="price" column="price" type="integer" />
        <field name="onHand" column="on_hand" type="integer" />
        <field name="availableOnDemand" column="available_on_demand" type="boolean"/>

        <one-to-many field="images" target-entity="Sylius\Bundle\CoreBundle\Model\VariantImage" mapped-by="variant">
            <cascade>
                <cascade-all />
            </cascade>`
        </one-to-many>
    </mapped-superclass>

</doctrine-mapping>

The entry for variable product in app/config/sylius.yml file is this:

sylius_variable_product:
    classes:
        variant:
            model: Acme\Bundle\VariableProductBundle\Model\Variant
            form: Sylius\Bundle\CoreBundle\Form\Type\VariantType

Can anyone tell me what I am missing?

It may be too late, but for the others : this worked for me.

In your MyBundle.php file (at the root of your bundle), you can override the build function like this :

    public function build(ContainerBuilder $container)
    {
        $mappings = array(
            realpath(__DIR__ . '/Resources/config/doctrine/model') => 'Namespace\MyBundle\Model',
        );
        $container->addCompilerPass(DoctrineOrmMappingsPass::createXmlMappingDriver($mappings, array('doctrine.orm.entity_manager'), 'sylius_core.driver.doctrine/orm'));
    }

Symfony by default does not look for entities in the "Model" directory. You should move your Variant model to "Entity" namespace. Sylius uses custom compiler passes to achieve this behavior, but for your app, it is not required at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM