save(array )" or "Model object->data(array)->save();"."/> save(array )" or "Model object->data(array)->save();".">

Home>Article>PHP Framework> How to use save method in thinkphp5

How to use save method in thinkphp5

WBOY
WBOY Original
2022-02-18 14:57:54 5316browse

In thinkphp5, the save() method is used to add a record to the specified data table. This method can only add one new record to the table at a time. Adding multiple records can be repeated. The syntax is "model Object-> save(array)" or "Model object-> data(array)-> save();".

How to use save method in thinkphp5

The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.

How to use the save method in thinkphp5

Add a single piece of data: save() method

Function: Add a record to the specified data table

The The method can only add one new record to the table at a time. Adding multiple records can be repeated. However, the saveAll() to be learned later can add multiple records at once.

Source code: /thinkphp/library/think/Model .php

The save method is one of the more complex methods in the Model class, because the save method plays multiple roles, not only for adding new data, but also for updating data.

Basic syntax:

Format 1: Write the data directly in the save method parameters

模型对象 -> save(数组);

Format 2: First generate the data object, and then use the save method to write directly to the table Both of these two syntaxes,

模型对象 -> data(数组) -> save();

, can complete the new operation. But the second grammatical structure is clearer, more readable, and more convenient to modify.

 data($data); //3.获取新增操作执行前:数据对象原始数据 $data_before = $model -> getData(); //4.查看新增操作执行前的数据对象:$model echo '查看新增操作执行前的数据对象:
'; dump($data_before); //5.将数据对象原始数据写入数据表中,返回影响记录数 //allowField(true)过滤post数组中的非数据表字段数据 $affected = $model -> allowField(true) -> save(); //6.获取新增操作执行后:数据对象原始数据 $data_after = $model -> getData(); //7.查看新增操作执行后的数据对象:$model echo '查看新增操作执行后的数据对象:
'; dump($data_after); //8. 获取新增记录的主键id,等价于: $model -> id $insert_ID = $affected ? $data_after['id'] : null; //6.验证是否新增成功 echo $affected ? '新增成功!新记录主键id是:'.$insert_ID : '新增失败!'; } }

save() method

class Index { public function index(){ //1.创建数据:与表中字段对应 $data = []; $data['name'] = '周星星'; $data['sex'] = 1; $data['age'] = 39; $data['salary'] = 3500; $data['dept'] = 3; $data['hiredate'] = date('Y-m-d',time()); $model = new Staff(); $affected = $model -> allowField(true) -> save($data); $data['name'] = '周星星'; $data['sex'] = 1; $data['age'] = 39; $data['salary'] = 3000; $data['dept'] = 3; $data['hiredate'] = date('Y-m-d',time()); $affected = $model -> allowField(true) -> setAttr('id',null) //清空ID -> isUpdate(false) //显式指定新增 -> save($data);

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use save method in thinkphp5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn