使用eloquent创建记录时,无法保存Laravel 4 created_at字段

时间:2014-07-09 09:05:27

标签: php mysql laravel timestamp eloquent

运行时版本:

PHP 5.3.10

Laravel 4.0

像这样创建表:

Schema::create('components', function($table)
{
    $table->engine = 'InnoDB';
    $table->increments('id');
    $table->string('component', 128);
    $table->string('description', 128);
    $table->integer('created_by')->unsigned();
    $table->timestamps();
});

模型定义:

class Component extends Eloquent {
    protected $table = 'components';
}

创建新记录:

$component = new Component;
$component->component = 'foo';
$component->description = 'foo description';
$component->created_by = 1;
$component->save();

数据库:

+------+------------+------------------+---------------+---------------------+---------------------+
| id   | component  | description      | created_by    | created_at          | updated_at          |
+------+------------+------------------+---------------+---------------------+---------------------+
| 1    | foo        | foo description  | 1             | 0000-00-00 00:00:00 | 2014-07-09 13:11:55 |
+------+------------+------------------+---------------+---------------------+---------------------+

DB :: getQueryLog()的结果:

insert into `pf_component` (`component`, `description`, `created_by`, `created_at`, `updated_at`) values ('foo', 'foo description',  1, '2014-07-09 13:11:55', '2014-07-09 13:11:55') {"bindings":["foo","foo description",1,"2014-07-09 13:11:55","2014-07-09 13:11:55"],"time":4.64,"name":"mysql"}

1 个答案:

答案 0 :(得分:0)

我尝试设置PDO选项并解决了问题,但我不知道为什么。

应用\设置\ database.php中

'connections' => array(
    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'test',
        'username'  => 'user',
        'password'  => 'pass',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'options'    => array(
            PDO::ATTR_EMULATE_PREPARES => true
        )
    ),
)