search

Home  >  Q&A  >  body text

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)

//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);


kevlinkevlin2675 days ago1843

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-07-31 06:34:11

    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
    2
  • 之间

    Teacher, I don't add a primary key. The save method reports this error: Field 'id' doesn't have a default value.

    之间 · 2019-06-27 14:12:09
  • Cancelreply