Home > Backend Development > PHP Tutorial > Yii framework associated query with usage analysis, yii framework with usage_PHP tutorial

Yii framework associated query with usage analysis, yii framework with usage_PHP tutorial

WBOY
Release: 2016-07-13 10:12:32
Original
1483 people have browsed it

Yii framework associated query with usage analysis, yii framework with usage

This article analyzes the usage of associated query with in Yii framework through examples. Share it with everyone for your reference. The specific method is as follows:

What is the difference between Yii framework related query and related query in mysql? Let’s take a look at it with you.

Yii’s related query is indeed a convenient thing. There is a lot of information on the Internet, but most of them are Ctrl+c, Ctrl+v. There are some things that no one has written an article to explain in detail. I’ll refer to them now. After reading many resources on the Internet and adding some of my own understanding, I wrote this article to provide some personal insights to beginners.

YII supports four types of relationships:

BELONGS_TO (belongs to): 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 B (for example, User has multiple Post);
HAS_ONE (has one): This is a special case of HAS_MANY, A can have at most one B (for example, User can have at most one Profile);
MANY_MANY: This corresponds to the many-to-many relationship in the database. Since most DBMS do not directly support many-to-many relationships, a relationship table is needed to split the many-to-many relationship into a one-to-many relationship.

Can newbies really understand this when they see this?

When I first started learning, I felt extremely dizzy. After repeated testing, I will give you a very straightforward explanation.
The existing user table is user and the blog table is blog. The blog belongs to a certain user, and the user will publish multiple blogs.
BELONGS_TO:
controller

Copy code The code is as follows:
$blogs = $blog_model->with('b_user')->find();

model The model here refers to blog_model
Copy code The code is as follows:
'b_user'=>array(self::BELONGS_TO, 'user', 'author')

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, the third parameter, the field in the main table used to store the primary key of the subtable (the field used in the blog table to store the primary key of the user table).

HAS_ONE:
controller
Copy code The code is as follows:
$user_blog = $user_model->with('u_blog')->find();

model The model here refers to user_model
Copy code The code is as follows:
'u_blog'=>array(self::HAS_ONE, 'blog', 'author')

After testing it, not only the author's information was found, but also a blog of the author was found.
The second parameter in u_blog: the name of the subtable, the third parameter, the field in the subtable used to store the primary key of the main table (the field in the blog table used to store the primary key of the user table).

HAS_MANY:
controller
Copy code The code is as follows:
$user_blogs = $user_model->with('u_blogs')->find();

model The model here refers to user_model
Copy code The code is as follows:
'u_blogs'=>array(self::HAS_MANY, 'blog', 'author')

After a test, not only the author's information was found, not only one of the author's blogs was found, but other blogs of the author were also found.
The second parameter in u_blogs: the name of the subtable, the third parameter, the field in the subtable used to store the primary key of the main table (the field in the blog table used to store the primary key of the user table).

MANY_TO_MANY:
I haven’t used this thing yet, and it seems that I won’t use it at all. I’ll test it out and then update it. . .

At this point, the most basic usage of Yii with is almost explained, but don’t you have any questions?
How to specify the subtable fields to be queried?
All articles of this user have been found in HAS_MANY. What if I only want to check 5 articles?
...Waiting for your questions.
So, without further ado, let’s solve the first problem
Copy code The code is as follows:
'u_blogs'=>array(self::HAS_MANY, 'blog', 'author','select' =>'gid,title,content')

Try this?
Second question
Copy code The code is as follows:
'u_blogs'=>array(self::HAS_MANY, 'blog', 'author','select' =>'gid,title,content','condition'=>'u_blogs.gid=2')

Done!

I believe that after reading these very novice explanations, you should suddenly realize it. As the saying goes, everything is difficult at the beginning. I believe you will understand the rest at a glance~~~

The following content is from the Yii manual:

Depending on the delay loading, 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 only applies 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 only applies to HAS_MANY and MANY_MANY relationships.
'limit': limit selection of data rows. This option does not apply to BELONGS_TO.
'offset': The offset of the data row. This option does not apply to BELONGS_TO.
'through': The name of the relationship that will be used as a bridge model when retrieving related data. Can be set to HAS_ONE and HAS_MANY only. This option is available since version 1.1.7.

The following is an example of a related object recorded for the 'Post' activity:

Copy code The 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'),
);

I hope this article will be helpful to everyone’s Yii framework programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/920617.htmlTechArticle Yii framework associated query with usage analysis, yii framework with usage This article analyzes the Yii framework associated query with usage with examples. Share it with everyone for your reference. The specific method is as follows: Yii framework off...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template