HasOne and hastwo are functions unique to Yii2 for multi-table join queries. we recommend that you use them when using multi-table join queries. This article describes how to use hasOne and hasOne in Yii2 and multiple-to-Multiple-join queries, for more information, see hasOne and hasOne, which are unique functions of Yii2 for multi-table join queries. we recommend that you use them when using multi-table join queries. This article describes how to use hasOne and hasOne in Yii2 and multiple-to-Multiple-join queries. For more information, see
Preface
HasOne and hastwo are functions unique to Yii2 for multi-table join queries. we recommend that you use them when using multi-table join queries. Why? In this way, the result of Association query will retain the table header sorting function of Yii2 and the input id in CheckboxColumn. if there are no other benefits, you need to explore it, I have discovered these two common benefits. Other associated queries, such as yiidbQuery or native SQL statement queries, are not found. when the query is displayed in the list, the table header is displayed in a black row.
HasOne and hassung multi-table Association queries in Yii2 can be easily used for reference in both documents and articles. but why should I write this article? the idea is actually very simple, because I didn't see the usage of multi-to-multi-join queries in the previous information, and I did not repeat others or myself. This article focuses on the usage of multi-to-multi-join queries, let you increase your knowledge.
Requirement Analysis
1. Use a query statement to display all the data in the list. The list contains one-to-one, one-to-many, and many-to-many relationships.
2. the table header sorting function of Yii2 and the input id in CheckboxColumn cannot be damaged.
1. one-to-one, one-to-multiple, multiple-to-many, and sort headers.
Code analysis
Many-to-many
Example: A customer can have multiple tags, and a single tag can be applied to multiple customers.
1. add the following code to TSales (model of customer table:
Public function getcommon_tag () {return $ this-> hasMany (CommonTag: className (), ['itemid' => 't_ id']) -> onCondition (['idtype' => "content"])-> joinWith (['tag']);}
Note: Here, the common_tag table is an intermediate table, and the joinWith (['tag']) associated tag table must be added after the common_tag table is associated with the customer table with hassag. OnCondition is the method for attaching conditions.
2. add the following code to join the tag table in CommonTag (intermediate table model), and use hasOne.
public function gettag(){ return $this->hasOne(Tag::className(), ['id'=>'tagid']);}
3. add the following code to the TSalesSearch (customer's Search model) to join the common_tag intermediate table: $ query-> joinWith (['common _ tag']);
4. the page output code is as follows:
[ 'attribute' => 'tag_id', 'value' => function ($model) { $_tag=$model->getRelatedRecords()['common_tag']; if(!empty($_tag)){ $tagName=""; foreach ($_tag as $key => $value) { $tagName.=$value['tag']['name'].'/'; } return rtrim($tagName,'/'); } },],
Note: $ model-> getRelatedRecords () is used to obtain the value of the [_ related: yiidbBaseActiveRecord: private] array.
One-to-multiple
1. in the previous multi-to-multi-use method, hasMany is a one-to-many usage. to implement one-to-many operations, remove the following joinWith, other configurations and output methods are the same.
One-to-one
1. one-to-one usage is also involved. hasOne is a one-to-one usage. the configuration is the same as that of has#, so we will not detail it here.
Notes
1. when the value queried by the association cannot be obtained using the code output below (common_tag.name), it should be that your customer table has this name field and this value is blank, after the names are duplicated, the fields in the master table are output first. the solution is to use $ model-> getRelatedRecords () to obtain the fields.
[ 'attribute' => 'tag_id', 'value' => 'common_tag.name' ]
The preceding figure shows the usage examples of hasOne, hasOne, and multiple-to-multiple join queries in Yii2. For more information, see other related articles in the first PHP community!