When using the saveAll method, the page shows that the insertion is successful, but when viewing it using the database, the data is not inserted* (it can be successfully inserted using save)
kevlin
kevlin 2017-07-30 21:31:31
0
2
1860
//1.实例化模型,创建模型对象
$user = new Tb_users();
//2.创建对象,采用对象方式
$data =[
    ['id' => 1008,'name' => '武则天','salary' => 7000,'dept' => '市场部','sex' => '女','hiredate' =>'2017-07-30'],
    ['id' => 1009,'name' => '嬴政','salary' => 7000,'dept' => '市场部','sex' => '男','hiredate' =>'2017-07-30'],
    ['id' => 1011,'name' => '孙悟空','salary' => 7000,'dept' => '市场部','sex' => '男','hiredate' =>'2017-07-30'],
];
$result = $user->saveAll($data,true);
dump($result);


kevlin
kevlin

reply all(1)
Peter_Zhu

Are you sure about the insert operation you performed?

If so, why do you need to give the primary key id?

If you give the primary key, the update operation will be automatically performed. If no data can be queried in the table, nothing will be done.

Please reconstruct your data to be inserted as follows:

$data =[
    ['name' => '武则天','salary' => 7000,'dept' => '市场部','sex' => '女','hiredate' =>'2017-07-30'],
    ['name' => '嬴政','salary' => 7000,'dept' => '市场部','sex' => '男','hiredate' =>'2017-07-30'],
    ['name' => '孙悟空','salary' => 7000,'dept' => '市场部','sex' => '男','hiredate' =>'2017-07-30'],
];

Please remove the id field in the example array and it will be automatically assigned by the system.

  • reply Teacher, I don't add a primary key. The save method reports this error: Field 'id' doesn't have a default value.
    之间 author 2019-06-27 14:12:09
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template