In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the usage of Yii framework association query with grammar". In the daily operation, I believe that many people have doubts about the usage of Yii framework association query with grammar. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Yii framework association query with grammar usage". Next, please follow the editor to study!
This paper gives an example to analyze the usage of Yii framework association query with. Share it with you for your reference. The specific methods are as follows:
What is the difference between the Yii framework association query and the association query in mysql? Here, the editor will join you to have a look.
The related query of Yii is indeed a convenient thing, and there are a lot of information on the Internet, but most of them are Ctrl+c,Ctrl+v, and some things have not been explained in detail. After referring to a lot of resources on the Internet, plus some of their own understanding, I wrote this article to provide some personal opinions for the vast number of beginners.
YII supports four types of relationships:
BELONGS_TO: if the relationship between tables An and B is one-to-many, then Table B belongs to Table A (for example, Post belongs to User)
HAS_MANY (multiple): if the relationship between tables An and B is one-to-many, then A has multiple Bs (for example, User has multiple Post)
HAS_ONE (with one): this is a special case of HAS_MANY, where 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 DBMS do not directly support many-to-many relationships, you need an association table to split many-to-many relationships into one-to-many relationships.
Can rookies really understand when they see this?
At the beginning, I said that I was extremely dizzy. After repeated tests, I would like to give you a very straightforward explanation.
The existing user table user and blog table blog, the blog belongs to a user, and the user will post multiple blogs.
BELONGS_TO:
Controller
The copy code is as follows:
$blogs = $blog_model- > with ('bounded username')-> find ()
Model, "model" here means blog_model.
The copy code is as follows:
'bounded username = > array (self::BELONGS_TO, 'user',' author')
The scope of application, when looking for the blog, you need to find out the author of the blog.
The second parameter in b_user: the table name of the child table, the third parameter, the field in the primary table used to store the primary key of the child table (the field in the blog table used to store the primary key of the user table).
HAS_ONE:
Controller
The copy code is as follows:
$user_blog = $user_model- > with ('uplink blog')-> find ()
Model, the model here refers to user_model.
The copy code is as follows:
'Used blogs = > array (self::HAS_ONE, 'blog',' author')
After testing, we found not only the author's information, but also a blog post of the author.
The second parameter in u_blog: the name of the child table, the third parameter, the field in the child table used to store the primary key of the primary table (the field in the blog table used to store the primary key of the user table).
HAS_MANY:
Controller
The copy code is as follows:
$user_blogs = $user_model- > with ('ubiquitous blogs')-> find ()
Model, the model here refers to user_model.
The copy code is as follows:
'Used blogs = > array (self::HAS_MANY, 'blog',' author')
After testing, we found not only the author's information, not only one of the author's blog, but also the author's other blogs.
The second parameter in u_blogs: the name of the child table, the third parameter, the field in the child table used to store the primary key of the primary table (the field in the blog table used to store the primary key of the user table).
MANY_TO_MANY:
This thing, ah, I have not used, it seems that basically will not use, wait for me to test, and then continue.
So far, the most basic usage of Yii with is almost done, but don't you have any questions?
How do I specify the child table fields to query?
All the user's articles are found in HAS_MANY. What if I only want to check 5 articles?
…… Waiting for you to ask questions.
So, let's cut the crap and solve the first problem.
The copy code is as follows:
Array (self::HAS_MANY, 'blog',' author','select'= > 'gid,title,content')
Try this?
The second question
The copy code is as follows:
'Used blogs = > array (self::HAS_MANY, 'blog',' author','select'= > 'gid,title,content','condition'= >' ubiquitous blogs. Giddy 2')
Got it!
I believe that after reading these very rookie explanations, I should suddenly realize. This so-called everything is difficult at the beginning. I believe you will understand the rest as soon as you read it.
The following is from the Yii manual:
There is a certain relationship during delayed loading, and the following options are available:
'group': string, GROUP BY clause. The default value is empty. Note that column references need to be prefixed with 'relationName''. (example: relationName.age). This option applies only to HAS_MANY and MANY_MANY relationships.
'having': string, HAVING clause. The default value is empty. Note that column references need to be prefixed with 'relationName''. (example: relationName.age). This option applies only to HAS_MANY and MANY_MANY relationships.
'limit selection of limit': data rows. This option cannot be applied to BELONGS_TO.
'offset of the offset': data row. This option cannot be applied to BELONGS_TO.
'through': will be used as the name of the relationship of the bridge's model when it gets the relevant data. Can be set to HAS_ONE and HAS_MANY only. This option is available from version 1.1.7.
The following is an example of an object related to the 'Post' activity record class:
The copy code is as follows:
Return array (
'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')
);
At this point, the study of "the usage of with syntax for Yii framework association query" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.