Home >PHP Framework >ThinkPHP >ThinkPHP database query Db class scenario analysis
"We are finally coming to the end of the framework analysis. This article will lead you to understand the mysteries of the models and views in the framework.
"
There are many models used in the daily development process, but in the development process I only know how to use it, but I don’t know how it is implemented internally. The model is something that is used regardless of the interface or the backend.
About the general trend of separation of front and backend views, most views in the framework are still used for backend development.
This article is also approaching the final stage of interpreting the framework. Next, Kaka will lead everyone to learn the mysteries of the Db class in the framework.
The picture below shows the brain map provided by Kaka. You can read the article based on this brain map.
What you must know before learning the model is the DB class, which also operates on the database.
There is such a configuration file in the framework, and in this configuration file there will be a series of information about the database configuration.
In the next process, Kaka will also simply create a database for demonstration.
There are also two classes in the core layer of the framework, namely the Db class and the Model class. These two classes are the next parsing objects. .
Before analyzing the corresponding relationship between the Db operation class and other classes, we first create a database as Demonstration use.
First, let’s take a look at the information of the Db class.
Through the above figure we can see part of the information about the Db class, which is to use some query methods of the Db class.
But when you come to the end of the Db class, you can see a familiar method __callStatic
.
This method should be familiar to readers who have been reading Kaka articles. This method is in the facade source code analysis An in-depth look is provided in that section.
The only thing you need to remember about this method is that it will be called when calling an undeclared static method.
As for call_user_func_array
The use of this function can be understood as this method is a built-in function and can be directly called to run the function, that is, the method can be run directly.
When you just looked at the annotation information of the Db class, you can see that the Db class uses the Connection
class , that is, the database connection class.
Enter this class and simply look at the constructor. The running sequence will be explained below.
There are two major scenarios for operating the controller in the framework. The first is Db class operation, and the second is Model operation.
Where Connection·is the connector, Query is the queryer, Builder is the sql generator, and exception is the exception class.
Knowing the above information will be helpful in the next understanding process. In the next section, the Db class library scenario will be analyzed.
Let’s analyze it from a simple case first. Database data.
Then go to the controller and write a simple query case. Before creating the controller, use the command to create a test controller. .
Perform simple query data in this controller.
The query results are as follows
In this case, you can see that the query method Db::query
is used. Next, we will conduct a simple analysis of this query method.
Then the execution process will come to the Db class. In this class, you can see that when the object accesses a non-existent static method, the __callStatic() method will be automatically called.
This method was explained in depth in the previous explanation of the facade.
As you can see from the above figure, when executing a static method that accesses a non-existent object, it will be executed to call_user_func_array calls the callback function and uses an array parameter as the parameter of the callback function
Then the code will be executed to static::connect()
This line of code, due to this type of Db It does not inherit any class, so the use of static is to call this class.
If the Db class inherits other classes, there will be a certain difference. This difference is about the static keyword. I will give you a little bit of unpopular knowledge to add. When a class inherits a class, in When the parent class uses the static keyword, the method of the subclass is called by default.
Switch database connection
Because there is no inheritance, you will come to the connect method of this class.
In this class, the result will first be returned as database configuration information.
Then the index query
will be obtained from the configuration information, and finally the string \think\db\Query
will be returned. It must be noted here that the characters returned are String, is not an instance of this class.
Then the third step will be executed to create a database connection object instance, and this step will be analyzed next.
You will then come to the fileThe actual execution is new \think\db\Query
, and eventually it will Return to execute the query and return the data set. The returned data isReturn object(think\db\Query)
About this$this->connection
is in this class The constructor is set.
Let’s take a brief look at this constructor. connection## is set directly in this constructor. #The value of this attribute, so it can be used in the above picture.
After the execution here is completed, the returned value will be called to the undeclared static method that was parsed from the beginning.
static::connect()
is the final returned valuestatic::connect() returns object(think\db\Query)
.
So the next code will be executed to the query method of thinkphp/library/think/db/Query.php
$sql
is the sql statement passed in Db::query(), and executes the query to return the data set
The last piece of code will execute the query of think\db\connector\Mysql Method
Next come to the query method of think\db\connector\Mysql
In this method The Central Committee did three things.
$this->initConnect
Initialize database connection$this->PDOStatement->execute( );
Execute the query Return the result set
Parse $this->initConnect Initialize the database connection
In this method It can be seen that a configuration information acquisition has been performed. First, you need to understand what this configuration information is. This configuration item is configured in the configuration file database. According to the information provided in the comments, you can see that it is mainly about the master-slave server. set. Generally, master-slave information is not configured in the framework. Here we will not analyze how the framework implements the master-slave configuration of the database. A judgment was made in this judgment The ID of the current database connection, and then the method to connect to the database is executed. This method will eventually return an instance information of object(PDO)#33.#$this->PDOStatement->execute(); Execute query
$this->PDOStatement, So it will be executed in the PDO class.
The third thing is to return the result set
The last step of execution up to this point is to return the result set. The methods used here are to query the bottom layer, and then parse it, and the final query result will be returned here. The final result will be returned to the__callStatic method here, and returned to the upper layer
$ resVariable.
Up to this point, the execution process of using Db query has been analyzed. , but the method encapsulated by the framework is not only query, other query methods can be simply analyzed according to the process of Kaka.
The last few processes in this section will be executed at the end, but there will be a little difference in the previous executions.
“Persistence in learning, persistence in blogging, and persistence in sharing are the beliefs that Kaka has always upheld since his career. I hope that Kaka’s articles in the huge Internet can bring you a little Silk help. My name is Kaka, see you next time.
”
The above is the detailed content of ThinkPHP database query Db class scenario analysis. For more information, please follow other related articles on the PHP Chinese website!