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