重写自定义Sylius包中的Variant类时出错

时间:2013-09-17 04:35:04

标签: php symfony sylius

我一直在尝试根据指南here派生Sylius的子类:VariableProductBundle:我自己的包中的Variant。

我创建了一个新的包,AcmeVariableProductBundle,它是通过概述here的方法从SyliusVariableProduct包派生的。

按照Sylius文档中列出的覆盖模型的步骤后,我出现以下错误:

  

Twig_Error_Runtime:在渲染模板期间抛出异常(“在链配置的命名空间中未找到类'Acme \ Bundle \ VariableProductBundle \ Model \ Variant'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“)在SyliusWebBundle:前端/主页:main.html.twig第2行。

我的课程定义如此:

namespace Acme\Bundle\VariableProductBundle\Model;

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

class Variant extends BaseVariant
{
    ...
}

我在以下位置创建了一个xml文件: Acme / Bundle / VariableProductBundle / Resources / config / doctrine / Variant.orm.xml 它看起来像这样:

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

app / config / sylius.yml文件中变量产品的条目是:

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

有谁能告诉我我错过了什么?

2 个答案:

答案 0 :(得分:2)

可能为时已晚,但对其他人来说:这对我有用。

在MyBundle.php文件中(在捆绑包的根目录下),您可以覆盖构建函数,如下所示:

    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'));
    }

答案 1 :(得分:1)

默认情况下,Symfony不会在“Model”目录中查找实体。您应该将Variant模型移动到“Entity”命名空间。 Sylius使用自定义编译器传递来实现此行为,但对于您的应用程序,根本不需要它。