This article analyzes the use of Yii framework association query with examples. Share it for your reference. The specific methods are as follows:
What is the difference between Yii framework association query and related query in mysql? Here, the editor will take a look with you.
Yii's association query is indeed a convenient thing, and there are a lot of information online, but most of them are Ctrl+c, Ctrl+v. Some things have not been published to explain them in detail. After referring to many online resources, and adding some of their own understanding, I wrote this article to provide some personal insights to beginners and friends.
YII supports four types of relationships:
BELONGS_TO(belongs): If the relationship between tables A and B is one-to-many, then table B belongs to table A (for example, Post belongs to User);
HAS_MANY(there are multiple): If the relationship between tables A and B is one-to-many, then A has multiple Bs (for example, User has multiple Posts);
HAS_ONE (there is one): This is a special case of HAS_MANY, A has at most one B (for example, User has at most one Profile);
MANY_MANY: This corresponds to a many-to-many relationship in the database. Since most DBMSs do not directly support many-to-many relationships, there is a need to have an association table to split the many-to-many relationship into one-to-many relationships.
Can novices really understand this when they see this?
When I was just starting out, I personally said I was extremely dizzy. After repeated tests, I will give you a very straightforward explanation.
The existing user table user and blog table blog. The blog belongs to a certain user, and the user will post multiple blogs.
BELONGS_TO:
controller
model Here the model refers to blog_model
Scope of application: When searching for a blog, you need to find out the author of the blog.
The second parameter in b_user: the table name of the subtable and the third parameter, the field in the main table that is used to store the subtable primary key (the field in the blog table that is used to store the user table primary key).
HAS_ONE:
controller
model The model here refers to user_model
After testing, it not only found out the author's information, but also found out a blog of the author.
The second parameter in u_blog: the name of the subtable table, the third parameter, the field in the subtable that is used to store the primary key of the main table (the field in the blog table that is used to store the primary key of the user table).
HAS_MANY:
controller
model The model here refers to user_model
After testing, it not only found the author's information, but also found the author's blog, but also found the author's other blogs.
The second parameter in u_blogs: the subtable table name, the third parameter, the field in the subtable that stores the primary key of the main table (the field in the blog table that stores the primary key of the user table).
MANY_TO_MANY:
I haven't used this thing yet, and it seems that I will basically not use it either. I will continue it after I test it. . .
At this point, the most basic usage of Yii with is almost done, but do you have no questions?
How to specify the subtable field to query?
All articles of this user were found in HAS_MANY. What if I only want to check 5 articles?
... Wait for you to ask a question.
Then, without saying much nonsense, solve the first problem
Try this way?
The second question
Get it done!
I believe that after reading these very novice explanations, I must have realized it suddenly. This is what is said to be difficult at the beginning. I believe you will understand the rest of the things~~~
The following content comes from Yii manual:
There is a certain relationship when delayed loading, and the following options are available:
'group': string, GROUP BY clause. The default value is empty. Note that the column reference needs to be prefixed with the 'relationName'. (For example: ). This option is only applicable to the HAS_MANY and MANY_MANY relationships.
'having': string, HAVING clause. The default value is empty. Note that the column reference needs to be prefixed with the 'relationName'. (For example: ). This option is only applicable to the HAS_MANY and MANY_MANY relationships.
'limit': limit selection of data rows. This option cannot be applied to BELONGS_TO.
'offset': The offset of the data row. This option cannot be applied to BELONGS_TO.
'through': The name of the relationship of the model that will be used as the bridge when obtaining the relevant data. Can be set to only HAS_ONE and HAS_MANY. This option is available since version 1.1.7.
Here is an example of an 'Post' activity record class related object:
'author'=>array(self::BELONGS_TO, 'User', 'author_id'),
'comments'=>array(self::HAS_MANY, 'Comment', 'post_id', 'with'=>'author', 'order'=>'create_time DESC'),
'tags'=>array(self::MANY_MANY, 'Tag', 'post_tag(post_id, tag_id)', 'order'=>'name'),
);
I hope this article will be helpful to everyone's yii framework programming.