1 |
|
1 |
|
1 |
|
1 |
|
In TP5, there are three methods for database access:
Native sql statement.
1 2 |
|
Query constructor
It should be noted that the above In the query method, find, select, insert, update, and delete are all query operations, and the others are auxiliary operations. The auxiliary operations return an object and support chain calls. But once the query operation is executed, it cannot be called further.
The order of different auxiliary operations has no effect, but the order of the same auxiliary operations will.
1 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Model
ORM Object Relation Mapping Object relational mapping
is mapped to our database through the model table, and then operate the database by operating the model.
We compare it with the query constructor
1 |
|
If it is a model operation, it can be implemented corresponding to the following code
1 2 |
|
This is outside the model, and also It is the value acquisition and setting operations in the controller, but inside the model, the following method is used:
1 2 3 |
|
CURD operation of the model
Creation
Db usage:
1 |
|
Model usage:
1 |
|
Summary:
save (dynamic) Returns: Number of affected records
create(static) Returns: model object instance (method can be called directly)
Read
Db:
1 |
|
Model:
1 |
|
It should be noted that find and select are methods of the query constructor, and get and all are methods of the model. But the model is based on the query constructor, so the model can call the find and select methods, but the query constructor cannot call the get and all methods
Summary:
Method Function Return value
get Query a single record Model object instance
find Query a single record Model object instance
all Query multiple records based on the primary key Array or data set containing model object instances
select Query multiple records based on conditions Containing model object instances Array or data set
Update
Db:
1 |
|
Model:
1 2 3 4 5 6 |
|
Summary:
Method Function Return value
save Update data Number of records affected
update Update data (static) Return model object instance
1 |
|
Delete
Db:
1 |
|
Model:
1 2 3 |
|
Summary:
Method Function Return value
delete Delete current data Number of records affected
destroy Delete specified data (static) Affected Number of records
Now that we have mastered the basic CURD operations of the model, let’s summarize the method differences:
Usage Db class model (dynamic) Model (static)
Create insert save create
Update update save update
Read a single find find get
Read multiple select select all
Delete delete delete destroy destroy
This article explains the database and model usage of ThinkPHP5, For more related content, please pay attention to php Chinese website.
Related recommendations:
A case study on thinkphp5.0 database operation
List some similarities and differences between ThinkPHP5 and ThinkPHP3
Create the simplest ThinkPhp project
The above is the detailed content of About database and model usage of ThinkPHP5. For more information, please follow other related articles on the PHP Chinese website!