Home > PHP Framework > ThinkPHP > ThinkPHP database query Db class scenario analysis

ThinkPHP database query Db class scenario analysis

咔咔
Release: 2021-01-11 13:51:25
Original
1838 people have browsed it
"

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.

"

Preface

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.

ThinkPHP database query Db class scenario analysis
Database execution process

1. Analysis of the correspondence between Db operation classes and other classes

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.

ThinkPHP database query Db class scenario analysis
Database configuration file

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. .

ThinkPHP database query Db class scenario analysis
Two classes that the core layer of the framework needs to know

Before analyzing the corresponding relationship between the Db operation class and other classes, we first create a database as Demonstration use.

ThinkPHP database query Db class scenario analysis
Demo database

First, let’s take a look at the information of the Db class.

ThinkPHP database query Db class scenario analysis
Comments on the 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.

ThinkPHP database query Db class scenario analysis
Calling undeclared static methods

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_arrayThe 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.

ThinkPHP database query Db class scenario analysis
The source code of the facade class

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.

ThinkPHP database query Db class scenario analysis
Connect to database class

Enter this class and simply look at the constructor. The running sequence will be explained below.

ThinkPHP database query Db class scenario analysis
Database configuration array creates Builder object

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.

2. Db class library scenario analysis

Let’s analyze it from a simple case first. Database data.

ThinkPHP database query Db class scenario analysis
Database simulation data

Then go to the controller and write a simple query case. Before creating the controller, use the command to create a test controller. .

ThinkPHP database query Db class scenario analysis
Created test controller

Perform simple query data in this controller.

ThinkPHP database query Db class scenario analysis
Use a simple case to query database data

The query results are as follows

ThinkPHP database query Db class scenario analysis
Query results

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.

ThinkPHP database query Db class scenario analysis
Static calling method in Db class

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.

ThinkPHP database query Db class scenario analysis
Switch database connection

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.

ThinkPHP database query Db class scenario analysis
Execute the query and return the data 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.

ThinkPHP database query Db class scenario analysis
Set up the connection

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).

ThinkPHP database query Db class scenario analysis
Static method call

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

ThinkPHP database query Db class scenario analysis
Execute the query and return the data set

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 $this->getResult($pdo, $procedure); Return the result set
ThinkPHP database query Db class scenario analysisExecute the query and return the data 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.

ThinkPHP database query Db class scenario analysisInitialize database connection
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.

ThinkPHP database query Db class scenario analysisDatabase deployment method: 0 centralized (single server), 1 distributed (master-slave server)
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.

ThinkPHP database query Db class scenario analysis
Connecting database method

#$this->PDOStatement->execute(); Execute query

The second thing to do is to execute the query. Next, let’s explain in detail how this is executed.

ThinkPHP database query Db class scenario analysisExecute query
When returning the pdo instance, this instance is assigned to the attribute

$this->PDOStatement, So it will be executed in the PDO class.

One thing you need to understand here is the execute method, which is used to execute statements that return multiple result sets, multiple update counts, or a combination of both.

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.

ThinkPHP database query Db class scenario analysisGet the data set array
The final result will be returned to the

__callStatic method here, and returned to the upper layer $ resVariable.

ThinkPHP database query Db class scenario analysis
Test case
ThinkPHP database query Db class scenario analysis
Final query result

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template