Symfony 自定义类型总是更改表
我添加了一个自定义类型,如:Symfony 自定义类型总是更改表,symfony,doctrine-orm,Symfony,Doctrine Orm,我添加了一个自定义类型,如: namespace My\SuperBundle\Types; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Platforms\AbstractPlatform; class Money extends Type { const MONEY = 'money'; public function getSqlDeclaration( array $fieldDeclaration
namespace My\SuperBundle\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class Money extends Type
{
const MONEY = 'money';
public function getSqlDeclaration(
array $fieldDeclaration,
AbstractPlatform $platform
) {
return 'DECIMAL(10,2)';
}
public function getName()
{
return self::MONEY;
}
}
在我的应用程序启动中:
namespace My\SuperBundle;
use Doctrine\DBAL\Types\Type;
use My\SuperBundle\Types\Money;
class MyBSuperBundle extends Bundle
{
public function boot()
{
//add custom quantity and wight types
$em = $this->container->get('doctrine.orm.entity_manager');
if(!Type::hasType(Money::MONEY)) {
Type::addType(Money::MONEY, 'My\SuperBundle\Types\Money');
}
}
}
但是,每次我都会用以下内容更新数据库:
php app/console doctrine:schema:update --dump-sql
我不断得到以下信息:
ALTER TABLE product_price CHANGE price price DECIMAL(10,2) DEFAULT NULL
use Doctrine\DBAL\Types\Type;
use My\SuperBundle\Types\Money;
class MyBSuperBundle extends Bundle
{
public function boot()
{
/* @var $em \Doctrine\ORM\EntityManager */
$entityManager = $this->container->get('doctrine.orm.entity_manager');
if( ! Type::hasType(Money::MONEY)) {
Type::addType(Money::MONEY, 'My\SuperBundle\Types\Money');
$entityManager
->getConnection()
->getDatabasePlatform()
->registerDoctrineTypeMapping('decimal', Money::MONEY);
}
}
}
除此之外,一切都非常好。数据库中的字段是正确的。
条令不断用相同的数据更新有什么原因吗?您没有告诉DBAL平台您的类型,因此很明显,DBAL模式自省实用程序无法识别它。要注册类型,可以执行以下操作:
ALTER TABLE product_price CHANGE price price DECIMAL(10,2) DEFAULT NULL
use Doctrine\DBAL\Types\Type;
use My\SuperBundle\Types\Money;
class MyBSuperBundle extends Bundle
{
public function boot()
{
/* @var $em \Doctrine\ORM\EntityManager */
$entityManager = $this->container->get('doctrine.orm.entity_manager');
if( ! Type::hasType(Money::MONEY)) {
Type::addType(Money::MONEY, 'My\SuperBundle\Types\Money');
$entityManager
->getConnection()
->getDatabasePlatform()
->registerDoctrineTypeMapping('decimal', Money::MONEY);
}
}
}
这应该可以防止DBAL抱怨模式差异。有一种使用配置的替代方法 config.yml:
doctrine:
dbal:
types: { money: My\SuperBundle\Types\Money }
connections:
your_connection_name:
mapping_types: { money: money }
资料来源:
requireSqlCommentHint(AbstractPlatform$platform)
并返回true
。这样,条令将记住习惯类型
namespace My\SuperBundle\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class Money extends Type
{
const MONEY = 'money';
public function getSqlDeclaration(
array $fieldDeclaration,
AbstractPlatform $platform
) {
return 'DECIMAL(10,2)';
}
public function getName()
{
return self::MONEY;
}
/**
* @inheritdoc
*/
public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}
}
来源:我对ZF2也有同样的问题 我解决了这个问题,删除了自定义类型名称中的连字符 错:
'doctrine_type_mappings' => [
'custom-type' => 'custom-type'
],
好:
有关Zend Framework 2中实现的更多详细信息:
我希望这能帮助一些人。看起来有一个bug我关闭了这个问题,因为它不完整,因为它是不可复制的。->registerDoctrineTypeMapping('decimal',Money::Money);或者->registerDoctrineTypeMapping('decimal(10,2)”,Money::Money)?应该没有具体的参数化。谢谢你的帮助。不幸的是,它没有起作用。我有三种类型(货币、重量和数量),它们都是十进制(10,2)的结果,是这样吗?在引导部分,我已经声明了所有的十进制,因为你认为使用内置的数字类型代替了吗?看起来我错过了“你只能把一个数据库类型映射到一个确切的理论类型。允许定义PostgreSQL之类的自定义类型的数据库供应商可以帮助克服这个问题。”(谢谢!请注意,如果不使用“connections”部分,可以将“mapping_types”条目直接放在“dbal”部分。