Home > Article > PHP Framework > ThinkPHP: Correct use of Db classes and models
It is found that many developers do not understand the correct usage posture of Db and model when using ThinkPHP5.*, especially in version 5.1. If you do not use the following correct posture, there are It is very likely that you will step into a trap.
The correct posture of Db
The following is the officially recommended usage of the Db class (that is, static method calls every time)
// 查询单个数据 Db::name('user')->where('id', 1)->find(); // 查询多个数据 Db::name('user')->where('id', '>', 1)->select(); // 写入新的数据 Db::name('user')->insert(['name' => '张三']); // 更新数据 Db::name('user')->where('id', 1)->update(['name' => '李四']); // 删除数据 Db::name('user')->delete(1);
Many developers To simplify the code, readers like to use the following code.
However, never use the code below in 5.1!
// 错误的用法 $user = Db::name('user'); // 查询单个数据 $user->where('id', 1)->find(); // 查询多个数据 $user->where('id', '>', 1)->select(); // 写入新的数据 $user->insert(['name' => '张三']); // 更新数据 $user->update(['name' => '李四']); // 删除数据 $user->delete(1);
Even using the helper function is still not recommended!
// 仍然是错误的用法 // 查询单个数据 db('user')->where('id', 1)->find(); // 查询多个数据 db('user')->where('id', '>', 1)->select(); // 写入新的数据 db('user')->insert(['name' => '张三']); // 更新数据 db('user')->update(['name' => '李四']); // 删除数据 db('user')->delete(1);
Many developers may wonder why it is wrong usage? The results I used are obviously fine, right? This just means that you haven’t stepped into the trap yet.
The real reason is that version 5.1 will not clear the previous query conditions after each query (5.0 will clear them every time), so the following usage is valid.
$user = Db::name('user'); // 查询分数大于80分的用户总数 $count = $user->where('score', '>', 80)->count(); // 查询分数大于80分的用户数据 $user->select();
You should understand after seeing this that when you use the same database query object instance, the query conditions will always be retained (that is, it will cause subsequent query conditions to be confused), and if you Multiple operations using helper functions or manual instantiation will be the same object instance, unless you manually clear it as below.
$user = Db::name('user'); // 查询分数大于80分的用户总数 $count = $user->where('score', '>', 80)->count(); // 清除查询条件(但不包括排序或者字段等信息) $user->removeOption('where'); // 查询所有用户数据 并按分数倒序排列 $user->order('score', 'desc')->select(); // 清除所有查询条件 $user->removeOption(); // 查询分数等于100的用户 $user->where('score', 100)->select();
Best practice: Use a new Db static query every time
The correct posture of the model
The design of the model In fact, just like Db, there is basically no need to manually instantiate it.
// 写入新的数据 $user = User::create(['name' => '张三']); // 更新数据 $user->update(['name' => '李四']); // 查询单个数据 $user = User::get(1); // 删除当前模型数据 $user->delete();
In the above code, we do not use any instantiation code, but use static method operations. The instantiation of the model is automatically completed by the system when querying or writing data. If you instantiate the model manually, it will cause the cost of repeated instantiation of the model.
Not recommended usage:
$user = new User; // 写入新的数据 $user->name = '张三'; $user->save();
$user = new User; $user->find(1); echo $user->name;
Recommended usage:
// 写入新的数据 User::create(['name' => '张三']); $user = User::get(1); echo $user->name;
So, please do not instantiate the model manually, and it is not recommended to use the model helper function.
Best practice: use static methods for both model query and creation
Now, do you understand the correct posture for using Db classes and models?
PHP Chinese website has a large number of free ThinkPHP introductory tutorials, everyone is welcome to learn!
This article is reproduced from: https://blog.thinkphp.cn/810719
The above is the detailed content of ThinkPHP: Correct use of Db classes and models. For more information, please follow other related articles on the PHP Chinese website!