如何覆盖SyliusCoreBundle模型用户

时间:2013-11-11 11:03:32

标签: symfony sylius

我尝试在模型User(SyliusCoreBundle / Model / User)中添加一个新字段“phone”。 避免触摸SyliusCoreBundle,

我创建了一个新的捆绑“ShopBundle”,它与其他sylius捆绑包一起覆盖基础用户类:

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

中添加了两个文件

的configuration.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);
    }


}

2 个答案:

答案 0 :(得分:4)

  1. 应该是User.orm.xml,而不是user.orm.xml
  2. 您必须在sylius_core -> classes -> user -> model node。
  3. 下配置该类
  4. 您不应该通过Sylius捆绑包启发您的捆绑包,并且绝对不要放在“Sylius”命名空间下。只需创建一个非常基本的Symfony包并将您的用户实体放在Entity命名空间下,Symfony就不会在Model下看到它。

答案 1 :(得分:1)

我想补充一点,为了使用Sylius灯具,您还必须配置用户资源:

sylius_resource:
  resources:
    sylius.user:
      classes:
        model: MyBundle\UserBundle\Entity\User