Create a new model
There are two methods:
One is to create it manually
1. Create a new model folder
2. Create a new file user.php. It is best that the name corresponds to the table name
3. Write the code
<?php namespace app\index\model; use think\Model; class User extends Model { //如果表名和文件名不是对应的,用下面代码修改 protected $table = 'think_user'; }
The second is to use the command:
>php think make:model index/Blog
Model instantiation
There are three ways:
1. Use static method
use app\index\model\User; $li= User::get(1);
2. Use loading class
use think\Loader; $user= Loader::model('user'); $li= $user::get(1);
3. Use system method
$user= model('User'); $li= $user::get(1);
Query operation of database
get means to query the data whose primary key is 1. The following is to query the data of name=thinkphp
//取出主键为1的数据 $user = User::get(1); // 使用数组查询 $user = User::get(['name' => 'thinkphp']);
You can also use the system query method
$user = new User(); $user->where('name', 'thinkphp')->find();
To query multiple pieces of data
Use the all() method
// 根据主键获取多个数据 $list = User::all('1,2,3'); // 或者使用数组 $list = User::all([1,2,3]); foreach($list as $key=>$user){ echo $user->name; } // 使用数组查询 $list = User::all(['status'=>1]); // 使用闭包查询 $list = User::all(function($query){ $query->where('status', 1)->limit(3)->order('id', 'asc'); }); foreach($list as $key=>$user){ echo $user->name; }
Or use the query method to view
$user = new User(); // 查询数据集 $user->where('name', 'thinkphp') ->limit(10) ->order('id', 'desc') ->select();
Query a certain field value
// 获取某个用户的积分 User::where('id',10)->value('score'); // 获取某个列的所有值 User::where('status',1)->column('name'); // 以id为索引 User::where('status',1)->column('name','id'); User::where('status',1)->column('id,name');
You can also use dynamic query
// 根据name字段查询用户 $user = User::getByName('thinkphp'); // 根据email字段查询用户 $user = User::getByEmail('thinkphp@qq.com');
New data
Use save method
$user = new User; $user->name = 'thinkphp'; $user->email = 'thinkphp@qq.com'; $user->save();
Use data batch method to save
$user = new User; $user->data([ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com' ]); $user->save();
The most commonly used is to import post or get data during instantiation, and then save it##I am I feel it is more important here
//如果需要过滤非数据表字段的数据,可以使用: $user = new User($_POST); // 过滤post数组中的非数据表字段数据 $user->allowField(true)->save(); //如果你通过外部提交赋值给模型,并且希望指定某些字段写入,可以使用: $user = new User($_POST); // post数组中只有name和email字段会写入 $user->allowField(['name','email'])->save();
Get the inserted data id
$user = new User; $user->name = 'thinkphp'; $user->email = 'thinkphp@qq.com'; $user->save(); // 获取自增ID echo $user->user_id;
Insert multiple pieces of data
$user = new User; $list = [ ['name'=>'thinkphp','email'=>'thinkphp@qq.com'], ['name'=>'onethink','email'=>'onethink@qq.com'] ]; $user->saveAll($list);
You can also use the assistant function
// 使用model助手函数实例化User模型 $user = model('User'); // 模型对象赋值 $user->data([ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com' ]); $user->save(); 或者进行批量新增: $user = model('User'); // 批量新增 $list = [ ['name'=>'thinkphp','email'=>'thinkphp@qq.com'], ['name'=>'onethink','email'=>'onethink@qq.com'] ]; $user->saveAll($list);
Data deletion , need to query first, then delete
Delete one item
$user = User::get(1); $user->delete();
Delete multiple items
User::destroy(1); // 支持批量删除多个数据 User::destroy('1,2,3'); // 或者 User::destroy([1,2,3]);
Delete according to conditions
// 删除状态为0的数据 User::destroy(['status' => 0]); 还支持使用闭包删除,例如: User::destroy(function($query){ $query->where('id','>',10); }); 或者通过数据库类的查询条件删除 User::where('id','>',10)->delete();
Data update , need to query first, and then update
$user = User::get(1); $user->name = 'thinkphp'; $user->email = 'thinkphp@qq.com'; $user->save();
Update according to conditions
$user = new User; // save方法第二个参数为更新条件 $user->save([ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com' ],['id' => 1]);
Post submission direct update
$user = new User(); // 过滤post数组中的非数据表字段数据 $user->allowField(true)->save($_POST,['id' => 1]); //如果你通过外部提交赋值给模型,并且希望指定某些字段写入,可以使用: $user = new User(); // post数组中只有name和email字段会写入 $user->allowField(['name','email'])->save($_POST, ['id' => 1]);
Forced update to prevent adding
$user = new User; $list = [ ['id'=>1, 'name'=>'thinkphp', 'email'=>'thinkphp@qq.com'], ['id'=>2, 'name'=>'onethink', 'email'=>'onethink@qq.com'] ]; $user->isUpdate()->saveAll($list);
Use database class to add
$user = new User; $user->where('id', 1) ->update(['name' => 'thinkphp']); 或者使用: $user = new User; $user->update(['id' => 1, 'name' => 'thinkphp']);
Recommended tutorial: "TP5"
The above is the detailed content of Model operations in thinkphp. For more information, please follow other related articles on the PHP Chinese website!