Home  >  Article  >  PHP Framework  >  An article explaining in detail how to add, delete, modify and query the database in Thinkphp5

An article explaining in detail how to add, delete, modify and query the database in Thinkphp5

青灯夜游
青灯夜游forward
2022-02-10 18:22:513632browse

How to operate the database in Thinkphp5 and perform addition, deletion, modification and query? The following article will give you a detailed understanding of the methods of adding, deleting, modifying and querying the database in Thinkphp5. I hope it will be helpful to you!

An article explaining in detail how to add, delete, modify and query the database in Thinkphp5

thinkphp standard data table design:

Create time field: create_time

Update time field :update_time

Delete time field: delete_time

Select int as the type, as shown below:

An article explaining in detail how to add, delete, modify and query the database in Thinkphp5

##[Related tutorial recommendations:

thinkphp framework

1. Create a model folder

Create a new folder named model in the secondary object directory under the application folder. This folder is the same as The corresponding controller and view directories are at the same level, as shown below:

An article explaining in detail how to add, delete, modify and query the database in Thinkphp5

#If there are multiple modules (such as front-end index, back-end admin) and the operating databases are similar, then you can The model model is placed in the common public module, as follows:

An article explaining in detail how to add, delete, modify and query the database in Thinkphp5

2. Create the model model class

1. Create the model object file in the model directory, generally The name of the model corresponds to the table name, for example:

表名 pre_user       --------------->  模型名 User.php
表名 pre_user_info  --------------->  模型名 UserInfo.php

2. Define the model model

<?php
namespace app\index\model;
use think\Model;
use think\Db;

class User extends Model{
	/**
     * 定义变量
     * 1.变量名称应与数据表中的字段名相同
     * 2.此处可根据需求省略,因为如果没有,thinkphp会自动在数据表中寻找的对应字段名
     */
	public $username;
	public $password;
}
?>

3. If the data model definition name is inconsistent with the table name, then additional definitions and declarations are required. As follows:

<?php
namespace app\index\model;
use think\Model;
use think\Db;

class User extends Model
{
	protected $table = "admin_user";//指定数据表名
    protected $pk = &#39;id&#39;;           //指定主键的字段
}
?>

3. Method of calling model model

//导入定义的数据模型类
use \app\index\model\User;

//方法一:
$res = User::get(1);

//方法二:
$user = new User;
$res = $user::get(1);	

//方法三:
use think\Loader;
$user = Loader::model("User");
$res = $user::get(1);

//方法四:
$user = model("User");       
$res = $user::get(1);

4. Query operation

get Get a record

$res = User::get(1);

all Get multiple records

1. No parameters are passed

$result = User::all(); //查询出所有记录

2. The parameter is n, n is a positive integer

$result = User::all(1); //查询出id为1的记录

3. The parameter is 'n1, n2, n3...'

$result = User::all(&#39;7, 8, 9, 10&#39;); //查询出id为7、8、9、10的4条记录

4. The parameters are [n1, n2, n3...]

$result = User::all([7, 8, 9, 10]); //查询出id为7、8、9、10的4条记录

find Query a certain item

 $res = User::where(&#39;id&#39;,&#39;1&#39;)->field(&#39;name&#39;)->find();

Not equal to

->where('id','neq',1)

##select multiple queries

$res = User::where(&#39;id&#39;,&#39;1&#39;)->field(&#39;name&#39;)->limit(2)->order(&#39;id DESC&#39;)->select();

value Query one by field

$res = User::where(&#39;id&#39;,&#39;1&#39;)->value(&#39;name&#39;);

Convert the result into an array

$res = $res->toArray();

Query number

//查询总条数
$res = User::count();
//按条件统计条数
$res = User::where(&#39;id&#39;,&#39;>&#39;,3)->count();
whereTime() time condition query

1. Get today’s information

db(&#39;table&#39;)->whereTime(&#39;c_time&#39;, &#39;today&#39;)->select();
//也可以简化为下面方式
db(&#39;table&#39;)->whereTime(&#39;c_time&#39;, &#39;d&#39;)->select();

2. Get yesterday’s information

db(&#39;table&#39;)->whereTime(&#39;c_time&#39;, &#39;yesterday&#39;)->select();

3. Get this Weekly information

db(&#39;table&#39;)->whereTime(&#39;c_time&#39;, &#39;week&#39;)->select();   
//也可以简化为下面方式
db(&#39;table&#39;)->whereTime(&#39;c_time&#39;, &#39;w&#39;)->select();

4. Get this month’s information

db(&#39;table&#39;)->whereTime(&#39;c_time&#39;, &#39;month&#39;)->select();   
//也可以简化为下面方式
db(&#39;table&#39;)->whereTime(&#39;c_time&#39;, &#39;m&#39;)->select();

5. Get last month’s information

db(&#39;table&#39;)->whereTime(&#39;c_time&#39;,&#39;last month&#39;)->select();

6. Get this year’s information

db(&#39;table&#39;)->whereTime(&#39;c_time&#39;, &#39;year&#39;)->select();    
//也可以简化为下面方式
db(&#39;table&#39;)->whereTime(&#39;c_time&#39;, &#39;y&#39;)->select();

7. Get last year’s information

db(&#39;table&#39;)->whereTime(&#39;c_time&#39;,&#39;last year&#39;)->select();

8. Date interval query

//根据时间戳查询今天到后天
db(&#39;table&#39;)->whereTime(&#39;time&#39;, &#39;between&#39;, [strtotime(date(&#39;Y-m-d&#39;)), strtotime(date(&#39;Y-m-d&#39;, strtotime(&#39;+2 day&#39;)))])->select();
根据日期查询今天到后天
db(&#39;table&#39;)->whereTime(&#39;time&#39;, &#39;between&#39;, [&#39;2020-3-28&#39;, &#39;2020-3-30&#39;])->select();

5. Add operation

1. Use the create() method to add

$res = User::create([
     &#39;name&#39;      => &#39;安阳&#39;,
     &#39;age&#39;       => 23,
     &#39;sex&#39;       => 1,
     &#39;password&#39;  => &#39;123456&#39;
 ]);

2. Add data and return the added primary key

$uid=UserModel::create([
     &#39;name&#39;      => &#39;安阳&#39;,
     &#39;age&#39;       => 23,
     &#39;sex&#39;       => 1,
     &#39;password&#39;  => &#39;123456&#39;
 ])->id;

You can also use the insertGetId method of the DB class, as follows:

$uid = User::insertGetId([
     &#39;name&#39;      => &#39;安阳&#39;,
     &#39;age&#39;       => 23,
     &#39;sex&#39;       => 1,
     &#39;password&#39;  => &#39;123456&#39;
 ]);

3. Add by instantiation

 $user = new User;
 $user->name =  &#39;安阳&#39;;
 $user->age =  23;
 $user->save();

4 , Filter the inserted fields by instantiation and return the number of inserted rows

 $user = new User;
 $data = [
     &#39;name&#39; => &#39;安阳&#39;,
     &#39;age&#39; => 23,
     &#39;email&#39; => &#39;123456@qq.com&#39;
 ];
 //只有name和age字段会写入
 $res = $user->allowField([&#39;name&#39;, &#39;age&#39;])->save($data);

5. The model uses allowField() to filter the data of non-data table fields

//定义模型对象,并传入post数据
$user = new User($_POST);
//过滤post数组中的非数据表字段数据
$user->allowField(true)->save();

6. The model uses allowField() to specify certain Field writing

$user = new User;
// post数组中只有name和email字段会写入
$user->allowField([&#39;name&#39;,&#39;email&#39;])->save($_POST, [&#39;id&#39; => 1]);

7. Use saveAll() for batch addition

user = new User;
$list = [
    [&#39;name&#39;=>&#39;安阳&#39;,&#39;email&#39;=>&#39;thinkphp@qq.com&#39;],
    [&#39;name&#39;=>&#39;小柒&#39;,&#39;email&#39;=>&#39;12345678@qq.com&#39;]
 ];
$user->saveAll($list);

You can also use the insertAll() method of the DB class to return the number of successfully added items

$res = User::insertAll([
     &#39;name&#39;      => &#39;安阳&#39;,
     &#39;age&#39;       => 23,
     &#39;sex&#39;       => 1,
     &#39;password&#39;  => &#39;123456&#39;
 ]);

Supplementary, other methods of filtering fields:

1. In DB operations, you can use strict to turn off strict field checking

Db::name(‘user’)->strict(false)->insert($data);

2. Use php's unset( ) method destroys variables

unset($data[‘file’]);

6. saveAll adds multiple pieces of data and returns the object list
 $user = new User;
 $data = [
     [
         &#39;name&#39; => &#39;安阳&#39;,
         &#39;age&#39; => 20,
         &#39;email&#39; => &#39;123456@qq.com&#39;
     ],
     [
         &#39;name&#39; => &#39;小柒&#39;,
         &#39;age&#39; => 25,
         &#39;email&#39; => &#39;ap555@qq.com&#39;
     ]
 ];
 $res = $user->allowField([&#39;name&#39;, &#39;age&#39;])->saveAll($data);

6. Update operation

1. update returns the number of affected rows

 $res = User::where([&#39;id&#39;=>1])->update([&#39;name&#39;=>&#39;安阳&#39;]);

2. setField updates a field individually

User::where(&#39;id&#39;,1)->setField(&#39;name&#39;,&#39;安阳&#39;);

3. setInc

//setInc(&#39;money&#39;,10)表示将money字段加上10
User::where([&#39;id&#39;=>1])->setInc(&#39;money&#39;, 10);

4. setDec

//setDec(&#39;money&#39;,10)表示将money字段减去10
User::where([&#39;id&#39;=>1])->setDec(&#39;money&#39;, 10);

5. Batch update requires the data to contain Primary key, return the update object list

$user = new User;
$res = $user->saveAll([
     [&#39;id&#39;=>1, &#39;name&#39; => &#39;安阳&#39;],
     [&#39;id&#39;=>2, &#39;name&#39; => &#39;小柒&#39;]
 ]);

7. Delete operation

1. Pass in the primary key, return the number of affected rows

$res = User::destroy(1);

2. Pass in the condition, return the number of affected rows

 $res = User::destroy([&#39;name&#39;=>&#39;安阳&#39;]);

3. Conditional deletion returns the number of affected rows

 $res = User::where([&#39;id&#39;=>1])->delete();

8. Transaction

1. Automatically control transaction processing

Db::transaction(function(){ 
    Db::table(&#39;order&#39;)->where([&#39;id&#39;=>1])->delete(); 
	Db::table(&#39;user&#39;)->where(&#39;id&#39;=>1)->setInc(&#39;money&#39;,10);	
});

2. Manually control transaction

Db::startTrans();//启动事务
try {
    Order::where([&#39;id&#39;=>1])->delete();
	User::where(&#39;id&#39;=>1)->setInc(&#39;money&#39;,10);
	Db::commit();//提交事务
} catch (Exception $e) {
	Db::rollback();	//回滚
}

9. Model model getter

The naming convention of the reader is:
->get the camel case name of the attribute name Attr

<?php
namespace app\index\model;
use think\Model;
class User extends Model
{		    
    //获取器:将性别的012修改为男、女、未知 返回
	public function getSexAttr($val)
	{
		switch ($val) {
            case 1:
                return &#39;男&#39;;
            case 2:
                return &#39;女&#39;;
            default:
            return &#39;未知&#39;;
		}
	}
   //获取器:格式化时间戳后返回
    public function getUpdateTimeAttr($val){
        if(!empty($val)){
			//如果是时间戳,就格式化
			if(!strtotime($val)) {
				return date(&#39;Y-m-d H:i:s&#39;,$val);
			}else{
				return $val;
			}
        }else{
			return &#39;&#39;;
		}
    }
}

Additional explanation: strtotime() parses the date and time description of any English text into a Unix timestamp, and returns the timestamp if successful, otherwise it returns FALSE (before PHP 5.1.0, this function returned -1 on failure )

十、model模型的修改器

<?php
namespace app\index\model;
use think\Model;
class User extends Model
{
	//修改器
	public function setTimeAttr()
	{
        return time();
	}
    /** 修改器:对密码字段加密之后存储
     * $val  第一个参数是密码
     * $data 第二个参数是添加的数据(可选)
     */
    public function setPasswordAttr($val,$data){
        if($val === &#39;&#39;) {
            return $val;
        }else{
            return md5($val.$data[&#39;email&#39;]);
        }
    }
}

十一、model模型的自动完成

auto       新增及更新的时候,自动完成的属性数组
insert     仅新增的时候,自动完成的属性数组
update   仅更新的时候,自动完成的属性数组

1、自动完成 

<?php
namespace app\index\model;
use think\Model;
class User extends Model
{
	//添加和修改时,都会自动完成的字段
    protected $auto = [&#39;addtime&#39;];

    public function setAddtimeAttr(){
        return time();
    }
}

2、添加数据时,自动完成 

<?php
namespace app\index\model;
use think\Model;
class User extends Model
{
	// 新增 自动完成
    protected $insert = [&#39;addtime&#39;];

    public function setAddtimeAttr(){
        return time();
    }
}

3、更新数据时,自动完成:

<?php
namespace app\index\model;
use think\Model;
class User extends Model
{
	// 更新 自动完成
    protected $update = [&#39;addtime&#39;];

    public function setAddtimeAttr(){
        return time();
    }
}

十二、自动完成时间戳

在数据库配置文件database.php中,有下列这项配置:

//自动写入时间戳字段
&#39;auto_timestamp&#39;  => false,
//如果开启(设置为true),则会自动完成所有表的时间戳,但是不建议这样,只在需要的地方设置更安全。

例如对用户表的时间戳自动完成,就在User的model中设置:

<?php
namespace app\index\model;
use think\Model;

class User extends Model{
    //开启自动完成时间戳功能
    protected $autoWriteTimestamp = true;
    //开启后,
    //添加数据时,默认自动完成的字段是:create_time和update_time
    //修改数据时,默认自动完成的字段是:update_time
    
    //如果数据表里不是这两个字段,则会报错。需要进行如下修改:
    protected $createTime = &#39;addtime&#39;;//修改默认的添加时间字段
    protected $updateTime = &#39;updtime&#39;;//修改默认的修改时间字段
    protected $updateTime = false;//当不需要这个字段时设置为false
}

Thinkphp更新时,自动更新update_time字段时间戳的方法:

1、使用update

User::update([&#39;name&#39;=>&#39;安阳&#39;],[&#39;id&#39;=>1]);

Thinkphp中update方法的源代码如下:

/**
    * 更新数据
    * @access public
    * @param array      $data  数据数组
    * @param array      $where 更新条件
    * @param array|true $field 允许字段
    * @return $this
    */
   public static function update($data = [], $where = [], $field = null)
   {
       $model = new static();
       if (!empty($field)) {
           $model->allowField($field);
       }
       $result = $model->isUpdate(true)->save($data, $where);
       return $model;
   }

2、使用save

$user=new User;
$user->isUpdate(true)->save([&#39;name&#39;=>&#39;安阳&#39;],[&#39;id&#39;=>1]);

 

十三、软删除

什么是软删除?

当删除某些记录时,有时我们需要假删除,只通过修改某个字段状态来标记该记录已删除,但实际上,数据库中还是存在这些记录的。假删除的应用场景还是比较多的,例如支付宝的收款记录,我们在APP上删除后,就不会再显示出来,你是不是以为真的删掉了,不会再留下任何痕迹?非也,非也,删除支付宝收款记录只是软删除,在支付宝的数据库中,实际上还保留有这些收款记录,如果你的收款涉嫌违规或者触犯法律,警方还是能通过支付宝的网警后台查看到的。

1、开启软删除

<?php
namespace app\index\model;
use think\Model;
use traits\model\SoftDelete;//引入软删除的类

class Order extends Model{
    //使用软删除
    //删除时,默认更新的字段是delete_time
    use SoftDelete;
    //如果数据表里不是delete_time这个字段,则会报错。需要进行如下修改:
    protected $deleteTime = &#39;deltime&#39;;
}

2、 控制器里软删除,返回影响的行数

 $res = Order::destroy(1);

执行删除后,就会更新delete_time字段,如果update_time字段也开启了自动完成,也会更新update_time字段。

3、如果开启了软删除,需要真正地删除数据,而不做软删除,用下面的方法

//destory()第二个参数传递true
$res = Order::destroy(1,true);

//delete()参数传递true
$orderData = Order::get(1);
$orderData ->delete(true);

4、查询已软删除的数据

$res = Order::withTrashed(true)->find(1);

5、查询仅包含已软删除的数据

$res = Order::onlyTrashed()->select();

推荐学习:《PHP视频教程

The above is the detailed content of An article explaining in detail how to add, delete, modify and query the database in Thinkphp5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete