如何覆盖 SyliusCoreBundle 模型用户 [英] How to override SyliusCoreBundle Model User

查看:28
本文介绍了如何覆盖 SyliusCoreBundle 模型用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to add a new field "phone" in model User (SyliusCoreBundle/Model/User). Avoiding to touch SyliusCoreBundle,

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

I create a new bundle 'ShopBundle' which is beside of the others sylius bundles to override base user class :

src/Sylius/Bundle/ShopBundle

在文件夹 ShopBundle 中:

in the folder 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 中添加了这一行

I add this line in app/AppKernel.php

new Sylius\Bundle\ShopBundle\SyliusShopBundle(),

最后,我确实赞扬:

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

我在数据库中没有要更新的内容.

I got nothing to update in database.

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

Which part i missed ? What can i do to make it works ? Thanks !!

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

I added two files in folder DependencyInjection

配置文件

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

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


}

推荐答案

  1. 应该是User.orm.xml,而不是user.orm.xml.
  2. 你必须在sylius_core下配置类->类->用户 ->模型节点.
  3. 您不应该通过 Sylius 捆绑包来激发您的捆绑包,并且绝对不要放在Sylius"命名空间下.只需创建一个非常基本的 Symfony 包并将您的用户实体放在 Entity 命名空间下,Symfony 不会在 Model 下看到它.
  1. It should be User.orm.xml, not user.orm.xml.
  2. You have to configure the class under sylius_core -> classes -> user -> model node.
  3. You should not inspire your bundle by Sylius bundles, and definitely not put in under "Sylius" namespace. Just create a very basic Symfony bundle and put your User entity under Entity namespace, Symfony won't see it under Model.

这篇关于如何覆盖 SyliusCoreBundle 模型用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆