ThinkPHP is an excellent PHP framework. Its core features are lightweight and fast, as well as powerful simplified programming and improved development efficiency. Among them, the four most important operations are add, delete, modify, and check, which are CRUD. Next, we will introduce the add, delete, modify, and check operations in ThinkPHP based on actual development cases.
1. Add
Adding new data is one of the most frequently used operations in ThinkPHP. In TP, new data can be added using a method in TP's model class library to insert data by instantiating the model class. The following is a general method:
$data = [ 'name' => '张三', 'age' => '18', 'gender' => '男' ]; $model = new UserModel; $res = $model->save($data);
The meaning of this code is to write a piece of data to the "User" table. The data is name is 'Zhang San', age is '18', and gender is 'male' . Among them, UserModel is a model file we created in advance. It inherits ThinkPHP's Model class and then makes relevant settings and definitions. The save method will return a Boolean value, indicating whether the writing is successful.
In actual development, we often encounter situations where multiple pieces of data are inserted at one time. In TP, we can use the batch insertion method provided by TP. The specific code is as follows:
$data = [ [ 'name' => '张三', 'age' => '18', 'gender' => '男' ], [ 'name' => '李四', 'age' => '22', 'gender' => '男' ], [ 'name' => 'Lucy', 'age' => '20', 'gender' => '女' ] ]; $model = new UserModel; $res = $model->saveAll($data);
2. Deletion
Deletion of data is an operation we often encounter in the background management system one. In TP, deleting data is also implemented through model classes. We can use the delete method to delete one or more pieces of data. The delete method can be used directly through the primary key, or it can be used to filter data using conditions (that is, where).
// 删除一条数据 $model = new UserModel; $res = $model->where(['id' => 1])->delete(); // 删除多条数据 $model = new UserModel; $ids = '1,2,3'; $res = $model->where(['id' => ['in', $ids]])->delete();
The meaning of the above code is to delete the data with id 1 from the User table, or delete the data with id 1, 2, and 3.
3. Modification
Modification of data is an operation we often use when processing business logic. TP provides the update method to modify data. The update method can also operate directly through the primary key, or use conditions to filter data.
// 修改一条数据 $model = new UserModel; $data = [ 'name' => '张三', 'age' => '20', 'gender' => '男' ]; $res = $model->where(['id' => 1])->update($data); // 修改多条数据 $model = new UserModel; $data = [ 'gender' => '女' ]; $ids = '2,3,4'; $res = $model->where(['id' => ['in', $ids]])->update($data);
The above code changes the name of the data with id 1 in the User table to 'Zhang San', the age to '20', and the gender to 'male'. The meaning of the latter code is to change the gender of the data with IDs 2, 3, and 4 to 'female'.
4. Query
Data query is one of our most commonly used operations. In TP, we can use the select method, find method, getField method and other methods in the model to query data. Commonly used query methods are as follows:
// 查询所有数据 $model = new UserModel; $res = $model->select(); // 查询一条数据 $model = new UserModel; $res = $model->where(['id' => 1])->find(); //查询指定字段 $model = new UserModel; $res = $model->getField('id,name,age');
The meaning of the above code is to query all data in the User table, or query the data with id 1, or query the id, name and age fields. What needs to be noted here is that when using the getField method, the returned result is an array with id as the key, name, and age as the value. If you want to modify the key or modify other fields as the value, you need to process it through the array function of tp.
Summary:
To sum up, CRUD is a very common operation in TP. Mastering these four operations can make us process background business logic more conveniently and quickly. Of course, TP has other more methods for these operations. I hope you can further explore them during the learning process and understand their underlying principles in depth. After all, mastering addition, deletion, modification, and search is the key to truly utilizing TP to its extreme.
The above is the detailed content of Examples to explain the addition, deletion, modification and search operations in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!