从迁移文件迁移Yii2数据

时间:2017-12-14 06:00:04

标签: php yii2-advanced-app yii-extensions yii2-model

我的迁移文件代码/值如下所示

$model->setAttributes(
  [
    'id' => '1',
    'title' => 'Tag test',
    'slug' => 'ssds',
    'status' => '1',
    'is_archived' => '0',
    'description' => 'sdsdfsd  asfda s fsd fsadfasfdsaf',
    'created_at' => '2017-12-14 00:00:00',
    'updated_at' => '2017-12-14 00:00:00',
    'created_by' => '2',
    'updated_by' => '2',
    'identifier' => 'ssds',
 ],

但是当我在作曲家中运行命令时,它会显示错误

  

异常:SQLSTATE [23000]:完整性约束违规:1048   列'created_by'不能为null正在执行的SQL是:INSERT   INTO标签(id,title,slug,status,is_archived,   description,created_at,updated_at,created_by,updated_by,   标识符)VALUES(1,'Tag test','ssds',1,0,'sdsdfsd asfda s   fsd fsadfasfdsaf',NOW(),NOW(),NULL,NULL,'ssds')   (E:\ XAMP-7 \ htdocs中\ 1712SAM \ dev的\厂商\ yiisoft \ yii2 \分贝\ Schema.php:636)

1 个答案:

答案 0 :(得分:0)

视情况而定,您将不安全属性设置为AR模型,并且为了正确工作,您需要将 setAttributes()函数中的第二个参数设置为 false http://www.yiiframework.com/doc-2.0/yii-base-model.html#setAttributes()-detail