Blogger Information
Blog 8
fans 0
comment 0
visits 7318
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5.24 实例演示模型闭包实现查询及软删除功能
Allen的php博客
Original
540 people have browsed it

一、建立用户数据表

26161551.png

二、创建模型类与控制器

模型User.php:

model.png

控制器User.php

user.png

三、代码实例:

app/moder/User.php

<?php

namespace app\model;

use think\Model;

use think\model\concern\SoftDelete;


class User extends Model

{

//设置数据库的名称

protected $table = 'user';


//设置默认主键

protected $pk = 'id';


//软删除:1.复制SoftDelete类到当前模型;2.添加软删除deleteTime属性; 3.设置软删除默认字段值

use SoftDelete;

protected $delete_time = 'delete_time';

    protected $defaultSoftDelete = 0;


}

app/admin/controller/User.php

<?php

namespace app\admin\controller;

use think\Controller; //导入基类

use app\model\User as UserModel; //导入模型类

use think\Db; 


class User  extends Controller

{

//实例化模型,依赖注入

public function instance(UserModel $user)

{

// dump($user->getName()); //查看模型名称是否正确

$user->name = '湖锦涛';

$user->password = md5(12345);

$user->nickname = '湖起波涛';

$user->email = '13141223@qq.com';

$user->save();

return '添加数据成功!id='.$user->id;

}


//模型查询

public function query()

{

//用闭包查询单条数据

$res = UserModel::get(function($query){

$query->where('id','=',5);

});

echo '查询单条数据:<br>';

dump($res);

echo '<hr>';

//用构造器查询单条数据

$res1 = UserModel::where('id','=',5)->find();

echo '用构造器查询单条数据:<br>';

dump($res1);

echo '<hr>';


//用闭包,查询多条数据

$res = UserModel::all(function($query){

$query->where('id','>',5)->limit(3);

});

echo '查询多条数据:<br>';

dump($res);

echo '<hr>';

//用构造器,查询多条数据

$res1 = UserModel::where('id','>',5)

->limit(3)

->select();

echo '用构造器查询多条数据:<br>';

dump($res1);

echo '<hr>';


}


//模型更新

public function update()

{

//用闭包(数据,条件),更新数据

UserModel::update(

['nickname'=>'春哥'],

function($query){

$query->where('id','=',4);

}

);

return '用闭包,更新数据成功!<hr>';

//用构造器,更新数据

$data = [

'nickname'=>'信春哥就对了',

'uid'=> Db::raw('uid+1')

];

UserModel::where('id','=',4)

->data($data)

->update();

echo "string"; '用构造器,更新数据成功!';

}


//模型新增

public function create()

{

//1. 用create(数据,字段)添加数据

$data = [

'name' => '马朝旭',

'password' => md5(12345),

'nickname' => '骏马奔腾',

'email' => '71134423@qq.com'

];

$field = ['name','password','nickname','email'];

UserModel::create($data,$field);

return '用create(数据,字段),添加数据成!<hr>';


//2. 用构造器添加数据

$data1 = [

'name' => '梁咏琪',

'password' => md5(12345),

'nickname' => '粱中宝玉',

'email' => '1322423@qq.com'

];

UserModel::insert($data1);

}


//模型删除

public function delete()

{

//用闭包,软删除数据

UserModel::destroy(function($query){

$query->where('id','>',0)->where('id','<',5);

});


//用构造器,软删除数据

UserModel::where('id','=',5)->delete();


//查询被软删除的数据

$res = UserModel::withTrashed()->select();

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

$res = UserModel::all();

dump($res);


//恢复单条,被软删除的数据

$res = UserModel::onlyTrashed()->find(2);

$res->restore();

// 恢复多条,被软删除的数据

$res = UserModel::update(

['delete_time'=>0],

function($query){

$query->where('id','>',0);

}

);


$res = UserModel::all();

dump($res);

}


}

以上代码均经过测试,代码可用

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!