使用CDbMigration将当前时间插入到MySQL表中 [英] Insert current time to MySQL table using CDbMigration
问题描述
如何在 FROM UNIXTIME =nofollow> CDbMigration
在Yii 1.x?
How should I use MySQL's FROM UNIXTIME
correctly in CDbMigration
in Yii 1.x?
我已经借用将当前时间转换为时间戳的解决方案,从 DateTime >这个答案,当打印时:
I have borrowed solution of converting current time given as timestamp, to MySQL's DateTime
field from this answer and when just printing it:
echo 'FROM_UNIXTIME('.$base.')'."\n";
echo 'FROM_UNIXTIME('.$sixDaysLater.')'."\n";
一切似乎都很好:
FROM_UNIXTIME(1418223600)
FROM_UNIXTIME(1418742000)
但是,当我尝试使用相同的技术作为我迁移的一部分:
But, when I'm trying to use the same technique as a part of my migration:
$this->insert('contents', array
(
'author_id'=>1,
'type'=>5,
'status'=>1,
'category'=>1,
'title'=>'title',
'body'=>'body',
'creation_date'=>'FROM_UNIXTIME('.$base.')',
'modification_date'=>'FROM_UNIXTIME('.$base.')',
'availability_date'=>'FROM_UNIXTIME('.$sixDaysLater.')',
'short'=>'short'
));
这个失败 - 也就是说,迁移很好,但是我可以在phpMyAdmin看到相关的字段由于此记录已填充零( 0000-00-00 00:00:00
),而不是预期值。
This fails -- that is, migration goes fine, but I can see in phpMyAdmin, that related fields for this record has been populated with zeros (0000-00-00 00:00:00
), not with the expected value.
我缺少什么?是,因为 insert
中的值正在编码/转义?
What am I missing? Is it, because values in insert
are being encoded / escaped?
推荐答案
您可以使用 CDBExpression :
You can use CDBExpression instead:
new CDbExpression("NOW()");
我的意思是:
'creation_date'=>new CDbExpression("NOW()")
或者如果要使用 FROM UNIXTIME
,您可以执行相同操作。
Or if you want to use FROM UNIXTIME
you can do the same.
CDbExpression 表示不需要转义的数据库表达式。
CDbExpression represents a DB expression that does not need escaping.
这篇关于使用CDbMigration将当前时间插入到MySQL表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!