gpt4 book ai didi

symfony - 如何覆盖 SyliusCoreBundle 模型用户

转载 作者:行者123 更新时间:2023-12-01 11:41:39 26 4
gpt4 key购买 nike

我尝试在模型用户 (SyliusCoreBundle/Model/User) 中添加一个新字段“phone”。避免接触 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);
}


}

最佳答案

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

关于symfony - 如何覆盖 SyliusCoreBundle 模型用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19904732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com