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
WBOYOriginal
2022-02-18 14:57:545435browse

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.

<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index {
  public function index(){
  //1.创建数据:与表中字段对应
  $data = [];
  $data[&#39;name&#39;] = &#39;周星星&#39;;
  $data[&#39;sex&#39;] = 1;
  $data[&#39;age&#39;] = 39;
  $data[&#39;salary&#39;] = 3500;
  $data[&#39;dept&#39;] = 3;
  $data[&#39;hiredate&#39;] = date(&#39;Y-m-d&#39;,time());
  //2.创建数据对象
  $model = (new Staff())-> data($data);
  //3.获取新增操作执行前:数据对象原始数据
  $data_before = $model -> getData();
  //4.查看新增操作执行前的数据对象:$model
  echo &#39;查看新增操作执行前的数据对象:<br />&#39;;
  dump($data_before);
  //5.将数据对象原始数据写入数据表中,返回影响记录数
//allowField(true)过滤post数组中的非数据表字段数据
  $affected = $model -> allowField(true) -> save(); 
  //6.获取新增操作执行后:数据对象原始数据
  $data_after = $model -> getData();
  //7.查看新增操作执行后的数据对象:$model
  echo &#39;查看新增操作执行后的数据对象:<br />&#39;;
  dump($data_after);
  //8. 获取新增记录的主键id,等价于: $model -> id 
  $insert_ID = $affected ? $data_after[&#39;id&#39;] : null;
  //6.验证是否新增成功
  echo $affected ? &#39;新增成功!新记录主键id是:&#39;.$insert_ID : &#39;新增失败!&#39;;    
  }
}

save() method

class Index {
  public function index(){
  //1.创建数据:与表中字段对应
  $data = [];
  $data[&#39;name&#39;] = &#39;周星星&#39;;
  $data[&#39;sex&#39;] = 1;
  $data[&#39;age&#39;] = 39;
  $data[&#39;salary&#39;] = 3500;
  $data[&#39;dept&#39;] = 3;
  $data[&#39;hiredate&#39;] = date(&#39;Y-m-d&#39;,time());
  $model = new Staff();
$affected = $model -> allowField(true) -> save($data); 
  $data[&#39;name&#39;] = &#39;周星星&#39;;
  $data[&#39;sex&#39;] = 1;
  $data[&#39;age&#39;] = 39;
  $data[&#39;salary&#39;] = 3000;
  $data[&#39;dept&#39;] = 3;
  $data[&#39;hiredate&#39;] = date(&#39;Y-m-d&#39;,time());
$affected = $model -> allowField(true)
                           -> setAttr(&#39;id&#39;,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