1. Add data
1.1 Add a piece of data
$user = new User; $user->name = 'thinkphp'; $user->email = 'thinkphp@qq.com'; $user->save(); $user = new User; $user->save([ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com' ]);
1.2 Filter non-data tables Field data
$user = new User; // 过滤post数组中的非数据表字段数据 $user->allowField(true)->save($_POST);
1.3 Specify certain field data
$user = new User; // post数组中只有name和email字段会写入 $user->allowField(['name','email'])->save($_POST);
1.4 Add multiple pieces of data
$user = new User; $list = [ ['name'=>'thinkphp','email'=>'thinkphp@qq.com'], ['name'=>'onethink','email'=>'onethink@qq.com'] ]; $user->saveAll($list);
1.5 Static method
$user = User::create([ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com' ]); echo $user->name; echo $user->email; echo $user->id; // 获取自增ID
2. Update data
2.1 Find and update
$user = User::get(1); $user->name = 'thinkphp'; $user->email = 'thinkphp@qq.com'; $user->save();
2.2 Update data directly
$user = new User; // save方法第二个参数为更新条件 $user->save([ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com' ],['id' => 1]);
2.3 Filter non-data tables Field
$user = new User; // 过滤post数组中的非数据表字段数据 $user->allowField(true)->save($_POST,['id' => 1]);
2.4 Specify certain fields
$user = new User(); // post数组中只有name和email字段会写入 $user->allowField(['name','email'])->save($_POST, ['id' => 1]);
2.5 Batch update data
$user = new User; $list = [ ['id'=>1, 'name'=>'thinkphp', 'email'=>'thinkphp@qq.com'], ['id'=>2, 'name'=>'onethink', 'email'=>'onethink@qq.com'] ]; $user->saveAll($list);
2.6 Static method
User::where('id', 1) ->update(['name' => 'thinkphp']);
2.7 Automatic recognition
2.7.1 Display updated data
// 实例化模型 $user = new User; // 显式指定更新数据操作 $user->isUpdate(true) ->save(['id' => 1, 'name' => 'thinkphp']);
2.7.2 Display new data
$user = User::get(1); $user->name = 'thinkphp'; // 显式指定当前操作为新增操作 $user->isUpdate(false)->save();
3. Delete data
3.1 Delete the current model
$user = User::get(1); $user->delete();
3.2 Delete based on primary key
User::destroy(1); // 支持批量删除多个数据 User::destroy('1,2,3'); // 或者 User::destroy([1,2,3]);
3.3 Delete conditionally
User::destroy(function($query){ $query->where('id','>',10); });
4. Query data
4.1 Obtain single data
// 取出主键为1的数据 $user = User::get(1); echo $user->name; // 使用查询构造器查询满足条件的数据 $user = User::where('name', 'thinkphp')->find(); echo $user->name;
4.2 Obtain multiple data
// 根据主键获取多个数据 $list = User::all('1,2,3'); // 或者使用数组 $list = User::all([1,2,3]); // 对数据集进行遍历操作 foreach($list as $key=>$user){ echo $user->name; }
// 使用查询构造器查询 $list = User::where('status', 1)->limit(3)->order('id', 'asc')->select(); foreach($list as $key=>$user){ echo $user->name; }
4.3 Get the value of a certain field or column
// 获取某个用户的积分 User::where('id',10)->value('score'); // 获取某个列的所有值 User::where('status',1)->column('name'); // 以id为索引 User::where('status',1)->column('name','id');
For more related ThinkPHP knowledge, please visitaskThinkPHP tutorial!
The above is the detailed content of How the Thinkphp framework operates on the database (summary). For more information, please follow other related articles on the PHP Chinese website!