With the continuous development of Web development, query correlation has become a very common requirement. Many frameworks also provide convenient query correlation interfaces. ThinkPHP is an extremely popular PHP framework. It provides powerful query correlation functions and can meet the various needs of developers. This article will explain in detail the query correlation function of ThinkPHP.
1. Model association
1.1 One-to-one association
One-to-one association means that there is only one record in each of the two data tables. In this case, use hasOne() and belongTo () function to associate. Suppose we have two tables, one is the user table and the other is the userinfo table. The structure of the two tables is as follows:
user: id name userinfo: id user_id age
The above two tables are related through the field user_id. We now want to find the user information in the user table and the age of the user. The specific operations are as follows:
Define a userinfo() method in the User model, with any method name.
//User模型 <?php class User extends Model{ public function userinfo(){ return $this -> hasOne('UserInfo', 'user_id'); } }
Define an age() method in the User model. This method actually defines an attribute that accesses the age field of the userinfo model.
//User模型 <?php class User extends Model{ protected $readonly = ['age']; public function userinfo(){ return $this -> hasOne('UserInfo', 'user_id'); } public function getAgeAttr($value, $data){ if(isset($data['userinfo'])){ return $data['userinfo']['age']; } return ''; } }
After completing the above code, we can use the find() method to query the user we want and their age:
//查询user表中id为1的用户 $user = User::get(1); echo $user -> name; echo $user -> age;
Note: In the above code, we use $ The readonly attribute, the $readonly attribute is an attribute provided by ThinkPHP, which can protect some attributes from being written to the database. In the above code, we set the age attribute as a read-only attribute, so that when $user -> age is accessed, the getAgeAttr method will be automatically called to query the age field in the userinfo model.
1.2 One-to-many association
One-to-many association means that one of the two data tables has multiple records, and the other has only one record. As in the following example:
order: id user_id order_no order_goods: id order_id name price
The above two tables are related through the field order_id. We now need to find the user's order information and corresponding product information in the user table. The specific operations are as follows:
Define an orders() method in the User model. This method indicates that a user has multiple orders.
//User模型 <?php class User extends Model{ public function orders(){ return $this -> hasMany('Order', 'user_id'); } }
Define a goods() method in the Order model. This method indicates that an order has multiple products.
//Order模型 <?php class Order extends Model{ public function goods(){ return $this -> hasMany('OrderGoods', 'order_id'); } }
After defining the above association, we can use the find() method to query the user's order and the products corresponding to each order:
//查询user表中id为1的用户的订单信息和订单的商品信息 $user = User::get(1, 'orders.goods'); var_dump($user -> orders[0] -> goods);
The last parameter ('orders.goods ') means querying all its Orders and Order-related Goods information at the same time.
2. Query association
2.1 Use association query
In addition to defining the association relationship at the model level, we can also implement association by calling the association attribute of the model layer Inquire. For example, we now want to query a user and its order information:
$user = User::get(1); $orders = $user -> orders; echo $user -> name; foreach($orders as $order){ echo $order -> order_no . "\n"; }
2.2 Delayed Association
If we don’t want to automatically query its association when querying a model, we can use delayed association to achieve this requirement. For example:
$user = User::with('orders')->get(1);
In the above code, when we set the $user variable, we defined the association to be obtained in the with() function. At this time, the query statement will not automatically query the association by default, but will wait for us The query will only be performed when using the association relationship.
2.3 Containing Associations
In addition to the delayed association above, we can also automatically include all associations by setting the true parameter after the with method to achieve our query needs. For example:
$user = User::with('orders')->find(1, true);
In the above code, we added a true parameter to the find() method. This parameter indicates that we want to include all associations of the user model.
The above is how to use ThinkPHP query correlation. ThinkPHP's query correlation function is very powerful and can meet most development needs.
The above is the detailed content of Detailed explanation of ThinkPHP's query correlation function. For more information, please follow other related articles on the PHP Chinese website!