Doctrine2-类型时间戳-默认值 [英] Doctrine2 - Type timestamp - Default value
问题描述
由于某些原因,我需要使用带有TIMESTAMP
字段的表.
For some reasons, I need to have tables with TIMESTAMP
fields.
我创建了自己的时间戳类型(Doctrine\DBAL\Types\Type
),效果很好.
I created my own Timestamp Type (Doctrine\DBAL\Types\Type
), it works fine.
但是当我尝试更新数据库结构时,我明白了.
But when I try to update my database structure, I got this.
命令行
ede80:~>php app/console doctrine:schema:update --force --complete
Updating database schema...
[Doctrine\DBAL\Exception\DriverException]
An exception occurred while executing 'ALTER TABLE MY_TABLE CHANGE DATE_CREATION DATE_CREATION timestamp DEFAULT NULL'
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'DATE_CREATION'
SQL查询
如果我在数据库上执行以下查询,则可以工作:
If I execute the following query on the database, I works :
ALTER TABLE MY_TABLE CHANGE DATE_CREATION DATE_CREATION timestamp DEFAULT CURRENT_TIMESTAMP
ORM.YML
但是当我尝试在MyTable.orm.yml
中进行更改时,就像这样:
But when I try to change it in MyTable.orm.yml
, like this :
dateCreation:
type: timestamp
nullable: true
column: DATE_CREATION
options:
default: CURRENT_TIMESTAMP
执行的查询是
ALTER TABLE MY_TABLE CHANGE DATE_CREATION DATE_CREATION timestamp DEFAULT 'CURRENT_TIMESTAMP'
它失败了.
如何设置有效的默认值,以便数据库的结构可以是最新的?
我已经尝试将options: default
设置为0
,NULL
,CURRENT_TIMESTAMP
,00:00:01
...
How can I set a working default value, so my database's structure can be up to date ?
I already tried to set options: default
to 0
, NULL
, CURRENT_TIMESTAMP
, 00:00:01
...
PHP 5.3.3 MySQL 5.1.63
PHP 5.3.3 MySQL 5.1.63
这是时间戳类型
我认为convert *()不太好.
I think that convert*() aren't that well.
<?php
namespace CNAMTS\PHPK\CoreBundle\Doctrine\Type;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
*/
class Timestamp extends Type
{
const TIMESTAMP = 'timestamp';
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getDoctrineTypeMapping('TIMESTAMP');
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return ($value === null) ? 0 : new \DateTime($value);
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return ($value === null) ? 0 : $value->format(\DateTime::W3C);
}
public function getName()
{
return self::TIMESTAMP;
}
}
推荐答案
这里是在表中具有多个时间戳字段的解决方案.
Here is the solution to have more than one timestamp field in the table.
您需要定义自己的时间戳类型,并将其添加到 AppKernel.php 中:
You need to have your own Timestamp type defined and added in AppKernel.php :
public function boot() {
parent::boot();
/**
* SQL type TIMESTAMP
*/
$em = $this->container->get('doctrine.orm.default_entity_manager');
Doctrine\DBAL\Types\Type::addType('timestamp', 'My\CoreBundle\Doctrine\Type\Timestamp');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('TIMESTAMP', 'timestamp');
}
在您的实体定义中, MyEntity.orm.yml :
dateCreation:
type: timestamp
nullable: false
column: DATE_CREATION
columnDefinition: TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00'
dateModification:
type: timestamp
nullable: false
column: DATE_MODIFICATION
columnDefinition: TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
在实体 MyEntity.php 中,有一个prePersist:
And in the entity MyEntity.php, have a prePersist :
public function doPrePersist()
{
$this->dateCreation = new \DateTime();
}
这就是我的工作方式:-) 谢谢大家的帮助!
That's how I made it work :-) Thx all for your help !
这篇关于Doctrine2-类型时间戳-默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!