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

标签 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"}

最佳答案

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

应用程序\config\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
        )
    ),
)

关于php - Laravel 4使用eloquent创建记录时无法保存created_at字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24649482/

相关文章:

mysql - 如何将默认数据插入到依赖于另一列的列中?

mysql - 如何从事件记录对象获取数据库 sql 值?

php - 从 laravel 中的表单输入将数据插入 hasone 表

php联系表单问题

php - 如何在 Laravel 5.2 中存储多选值

php - 什么是 PHP 中的工厂设计模式?

php - "Temporary failure in name resolution": Symfony 5. 3 使用 Doctrine 和 Docker 连接到 MariaDB

php - 隐藏已弃用的错误消息不起作用

angularjs - 如何使用 AngularJS 在 Laravel 4 中保护 API 访问

php - Eloquent ORM 和 Laravel 中的连接问题