How to override SyliusCoreBundle Model User

半城伤御伤魂 提交于 2019-12-22 10:52:55

问题


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

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

src/Sylius/Bundle/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

In file src/Sylius/Bundle/ShopBundle/Model/User.php, I have :

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

In file src/Sylius/Bundle/ShopBundle/Resources/config/doctrine/model/user.orm.xml, I have :

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

In file src/Sylius/Bundle/ShopBundle/SyliusShopBundle.php, I have :

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

I add this line in app/AppKernel.php

new Sylius\Bundle\ShopBundle\SyliusShopBundle(),

Final, I do commend like :

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


I added two files in folder 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);
    }


}

回答1:


  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.



回答2:


I would add that in order to use Sylius fixtures you have to configure the user resource too:

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


来源:https://stackoverflow.com/questions/19904732/how-to-override-syliuscorebundle-model-user

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!