Home >PHP Framework >ThinkPHP >How to use ORM to perform multi-table related queries in ThinkPHP6
With the continuous development of software development technology, many developers are pursuing not only the efficiency and practicality of the code, but also the readability and maintainability of the code. ORM (Object-Relational Mapping) can obviously meet this demand. ORM can help us map relational data in the database to relationships between objects, thereby reducing the amount of database interaction code required by programmers. The ThinkPHP6 framework uses ORM technology to allow developers to easily operate relational databases. This article will introduce how to use ORM to perform multi-table related queries in ThinkPHP6.
1. Use ORM for single-table query
Before using ORM for multi-table related query, we need to first master how to use ORM for single-table query.
It is very convenient to use ORM to query record operations in ThinkPHP6. Just instantiate the corresponding model in the controller and then call the methods in the model. For example, we need to query the data in a student table.
First, create a model file named Student in the application directory. In this file, define the attributes of the student table by inheriting the hinkModel class.
<?php namespace appmodel; use thinkModel; class Student extends Model { }
Then instantiate the Student model in the controller and call the existing query method in the model to complete the single table query.
<?php namespace appcontroller; use appmodelStudent; class StudentController { public function index() { $student = new Student(); $list = $student->select(); dump($list); } }
After running the index method in the controller, we can see all the record information in the student table on the page, which is very convenient.
2. Use ORM to perform simple multi-table correlation queries
In ThinkPHP6, you do not need to manually write SQL joint table query statements to perform database correlation queries. ORM will automatically parse the relationships between models, and then automatically generate corresponding SQL query statements. Developers do not need to understand the detailed syntax of SQL statements. They only need to define the relationship in the model, and then call the query method in the controller to complete the multi-table joint query.
1. One-to-one correlation query
When there is a one-to-one relationship between two database tables, we can use one-to-one correlation query. For example, in the student table, a student can only correspond to one class, so there is a one-to-one relationship between the two tables. We can use ORM to query the information in the student table and query the class information he is in.
First, define the one-to-one relationship between the two tables in the Student model.
<?php namespace appmodel; use thinkModel; class Student extends Model { public function grade() { return $this->hasOne('Grade', 'id', 'grade_id'); } }
In the association method, the first parameter 'Grade' represents the table to be associated, the second parameter 'id' represents the associated field in the Grade table, and the third parameter 'grade_id' represents Related fields in the Student table.
Then call the with method in the model in the controller to query all the information in the student table and its class information.
<?php namespace appcontroller; use appmodelStudent; class StudentController { public function index() { $list = Student::with('grade')->select(); dump($list); } }
By using ORM, we can easily query all the information in the student table and related information about the class where it is located. This method is a one-to-one related query.
2. One-to-many correlation query
When there is a one-to-many relationship between two database tables, we can use one-to-many correlation query. For example, in the student table, a class can have multiple students, and there is a one-to-many relationship. We can use ORM to query class information and query all student information in the class.
First, define the one-to-many relationship between the two tables in the Grade model.
<?php namespace appmodel; use thinkModel; class Grade extends Model { public function student() { return $this->hasMany('Student', 'grade_id', 'id'); } }
In the association method, the first parameter 'Student' represents the table to be associated, the second parameter 'grade_id' represents the associated field in the Student table, and the third parameter 'id' represents Related fields in the Grade table.
Then call the with method in the model in the controller to query the information of all students in the class.
<?php namespace appcontroller; use appmodelGrade; class GradeController { public function index() { $list = Grade::with('student')->select(); dump($list); } }
This method is a one-to-many related query.
3. Use ORM to perform multiple one-to-many association queries
When we need to query multiple one-to-many association tables during development, we can use the nested association method of the model. For example, if we now need to query information about a school that has multiple classes and each class has multiple students, we need to use the nested association method.
First, define a one-to-many relationship in the Teacher model.
<?php namespace appmodel; use thinkModel; class Teacher extends Model { public function grade() { return $this->hasMany('Grade', 'teacher_id', 'id'); } }
Then define a one-to-many association in the Grade model.
<?php namespace appmodel; use thinkModel; class Grade extends Model { public function student() { return $this->hasMany('Student', 'grade_id', 'id'); } public function teacher() { return $this->belongsTo('Teacher', 'teacher_id', 'id'); } }
In the association method, belongsTo indicates that the foreign key of the current model is associated with the primary key of the model's associated table, that is, the primary key of the Teacher table corresponds to the teacher_id field in the Grade table.
Finally, call the with method in the model in the controller to complete multiple one-to-many associated queries.
<?php namespace appcontroller; use appmodelTeacher; class TeacherController { public function index() { $list = Teacher::with([ 'grade' => function($query) { $query->with('student'); } ])->select(); dump($list); } }
Through the above series of operations, we can complete multiple one-to-many related queries.
Summary
The emergence of ORM technology has reduced the difficulty of program development to a certain extent. The ORM of the ThinkPHP6 framework makes developers more flexible and convenient in database operations. Understanding and mastering the operating methods of ORM can make us more efficient and accurate in data query, which will bring great convenience to our subsequent development.
The above is the detailed content of How to use ORM to perform multi-table related queries in ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!