Home  >  Article  >  PHP Framework  >  How to use the query correlation function of ThinkPHP

How to use the query correlation function of ThinkPHP

WBOY
WBOYforward
2023-06-03 08:01:171691browse

1. Model association

1.1 One-to-one association

One-to-one association is two data There is only one record in each table. In this case, the hasOne() and belongTo() functions are used to associate. Suppose we have two data tables, one is called the user table (user table) and the other is the user information table (userinfo table). Their respective structures are as follows:

user:
id
name

userinfo:
id
user_id
age

The above two tables are represented by fields user_id is associated. Now we need to query the user information and corresponding age in the user table. The steps 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(&#39;UserInfo&#39;, &#39;user_id&#39;);
}
}

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 = [&#39;age&#39;];

public function userinfo(){
return $this -> hasOne(&#39;UserInfo&#39;, &#39;user_id&#39;);
}

public function getAgeAttr($value, $data){
if(isset($data[&#39;userinfo&#39;])){
return $data[&#39;userinfo&#39;][&#39;age&#39;];
}
return &#39;&#39;;
}
}

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(&#39;Order&#39;, &#39;user_id&#39;);
}
}

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(&#39;OrderGoods&#39;, &#39;order_id&#39;);
}
}

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, &#39;orders.goods&#39;);
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 Using association query

We can access the association attributes defined in the model layer, Implement related queries without defining relationships at the model layer level. 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 correlation

If we don’t want to automatically query its correlation when querying a model Relationships, you can use delayed correlation to achieve this requirement. For example:

$user = User::with(&#39;orders&#39;)->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(&#39;orders&#39;)->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 the detailed content of How to use the query correlation function of ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete