3

我尝试在模型用户(SyliusCoreBundle/Model/User)中添加一个新字段“电话”。避免接触 SyliusCoreBundle,

我在其他 sylius 捆绑包旁边创建了一个新捆绑包“ShopBundle”,以覆盖基本用户类:

src/Sylius/Bundle/ShopBundle

在文件夹 ShopBundle 中:

> /Controller(empty)

> /DependencyInjection(empty)

> /Model

>         /User.php
> /Resources

>         /config/doctrine/model/user.orm.xml
>         /config/service.xml (empty)
> SyliusShopBundle.php

在文件src/Sylius/Bundle/ShopBundle/Model/User.php中,我有:

    <?php

namespace Sylius\Bundle\ShopBundle\Model;

use Sylius\Bundle\CoreBundle\Model\User as BaseUser;

class User extends BaseUser
{
  protected $mobile;

  /**
   * {@inheritdoc}
   */
  public function setMobile($mobile)
  {
    $this->mobile = $mobile;
  }
  /**
   * {@inheritdoc}
   */
  public function getMobile()
  {
    return $this->mobile;
  }
}

在文件src/Sylius/Bundle/ShopBundle/Resources/config/doctrine/model/user.orm.xml中,我有:

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                                      http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <mapped-superclass name="Sylius\Bundle\ShopBundle\Model\User" table="sylius_user">

        <field name="mobile" column="mobile" type="string" nullable="true" />

    </mapped-superclass>

</doctrine-mapping>

在文件src/Sylius/Bundle/ShopBundle/SyliusShopBundle.php中,我有:

 class SyliusShopBundle extends Bundle
{
    /**
     * Return array with currently supported drivers.
     *
     * @return array
     */
    public static function getSupportedDrivers()
    {
        return array(
            SyliusResourceBundle::DRIVER_DOCTRINE_ORM
        );
    }
}

我在 app/AppKernel.php 中添加了这一行

new Sylius\Bundle\ShopBundle\SyliusShopBundle(),

最后,我推荐如下:

php app/console doctrine:schema:update --dump-sql

我在数据库中没有什么要更新的。

我错过了哪一部分?我该怎么做才能使它起作用?谢谢 !!


我在文件夹 DependencyInjection 中添加了两个文件

配置.php

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('sylius_shop');

        $rootNode
            ->addDefaultsIfNotSet()
            ->children()
                ->scalarNode('driver')->cannotBeOverwritten()->isRequired()->cannotBeEmpty()->end()
            ->end()
        ;

        $this->addClassesSection($rootNode);

        return $treeBuilder;
    }

    /**
     * Adds `classes` section.
     *
     * @param ArrayNodeDefinition $node
     */
    private function addClassesSection(ArrayNodeDefinition $node)
    {
        $node
            ->children()
                ->arrayNode('classes')
                    ->addDefaultsIfNotSet()
                    ->children()
                        ->arrayNode('user')
                            ->addDefaultsIfNotSet()
                            ->children()
                                ->scalarNode('model')->defaultValue('Sylius\\Bundle\\ShopBundle\\Model\\User')->end()
                            ->end()
                        ->end()
                    ->end()
                ->end()
            ->end()
        ;
    }
}

SyliusShopExtension.php

<?php

namespace Sylius\Bundle\ShopBundle\DependencyInjection;

use Sylius\Bundle\ResourceBundle\DependencyInjection\SyliusResourceExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;


class SyliusShopExtension extends SyliusResourceExtension
{
    /**
     * @var array
     */
    private $bundles = array();

    /**
     * {@inheritdoc}
     */
    public function load(array $config, ContainerBuilder $container)
    {
        $this->configDir = __DIR__.'/../Resources/config';

        $this->configure($config, new Configuration(), $container, self::CONFIGURE_LOADER | self::CONFIGURE_DATABASE | self::CONFIGURE_PARAMETERS);
    }


}
4

2 回答 2

4
  1. 应该是User.orm.xml,不是user.orm.xml
  2. 您必须在sylius_core -> classes -> user -> model节点下配置类。
  3. 你不应该通过 Sylius 包来激发你的包,并且绝对不要放在“Sylius”命名空间下。只需创建一个非常基本的 Symfony 包并将您的 User 实体放在Entity命名空间下,Symfony 不会在Model.
于 2013-11-13T09:06:21.017 回答
1

我要补充一点,为了使用 Sylius 固定装置,您还必须配置用户资源:

sylius_resource:
  resources:
    sylius.user:
      classes:
        model: MyBundle\UserBundle\Entity\User
于 2014-09-02T09:16:17.597 回答