How to override SyliusCoreBundle Model User

2019-05-16 12:04发布

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


}

2条回答
▲ chillily
2楼-- · 2019-05-16 12:24
  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.
查看更多
Ridiculous、
3楼-- · 2019-05-16 12:30

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
查看更多
登录 后发表回答