The usage of Hasone, Hasmany and many-to-many related queries in Yii2

Source: Internet
Author: User
This article mainly introduces the YII2 in the Hasone, Hasmany and many-to-many correlation query usage detailed, has certain reference value, now shares to everybody, has the need friend can refer to

Objective

HasOne, Hasmany is a function of YII2, which is used for multi-table association queries, and is recommended when using multi-table association queries. Why? Because this way the results associated with the query will retain Yii2 's own header sorting function, as well as the ID value of input in Checkboxcolumn, as to whether there are other benefits you need to dig, the author now found the two common benefits. Other related queries, such as yiidbquery queries or native SQL query queries are not, query out when the list is displayed, the table header row black.

Yii2 Hasone, hasmany Multi-Table association query, whether it is a document or the article can easily find the reference parent, but why do I have to write this article, the idea is actually very simple, because in the previous information I did not see many of the use of multi-correlation query, do not repeat others, Also repeat yourself, the focus of this article is to talk about the use of multi-correlation query, let you rise knowledge.

Demand analysis

1, the use of a query statement will be able to display the list of data all out, the list contains one-to-one, one-to-many, and many-to-many relationships.

2, can not destroy Yii2 's own header sorting function, as well as the ID value of input in Checkboxcolumn.

1, one-to-one, one-to-many, many-to-many, table header sort.

2. The ID value of input in Checkboxcolumn.

Code Analysis

Many-to-many

Example: A customer can have multiple labels, one label can be for multiple customers.

1. Add the following code to the Tsales (model of the 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 the intermediate table, and the Common_tag intermediate table is associated with the Customer table after hasmany with the Joinwith ([' tag ']) associated with the label table. Oncondition is the method that attaches the condition.

2, in Commontag (intermediate table model) to add the following code associated with the label table, with Hasone on the line.

Public Function Gettag () {  return $this->hasone (Tag::classname (), [' id ' = ' tagid ']);}

3, in Tsalessearch (Customer search model) to add the following code associated 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 the value used to get an array of [_related:yiidbbaseactiverecord:private].

One-to-many

1, in front of many-to-many usages involved in the Hasmany is a one-to-many usage, to achieve a one-to-many words on the back of the joinwith can be removed, the other configuration and output the same way.

One

1, one-to-one usage before also involved, hasone belong to one-to-one usage, configuration and hasmany, here is not detailed.

Precautions

1, when the associated query out value using the following code output (Common_tag.name) to get the value of the time, it should be your client table exists in this name field, and this value is empty, after the names of the first output of the Main table field, solution by: $model Getrelatedrecords () gets.

[' attribute ' = ' tag_id ', ' value ' = ' common_tag.name ']

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.