save(array )" or "Model object->data(array)->save();"."/> save(array )" or "Model object->data(array)->save();".">
search
HomePHP FrameworkThinkPHPHow to use save method in thinkphp5

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor