Yii Correlation Query related issues

Source: Internet
Author: User
Yii Related query problem
Three sheet A B C

AB is a pair of multiple
BC is a one-to

How to query a record that satisfies the final display of C
------Solution--------------------
A---User table (with user_name field)
B---Article table (with user_id field)
C---article_content table (with article_content_id field)

Condition: Query user's username through the article_content_id field of the Article_content table
Requires two relation, add one to the model/comment.php:

Public Function relations () {
Return Array (
....
' UserInfo ' = Array (self::belogns_to, ' User ', ' user_id '),
);
}


Ibid., add in model/commentcontent.php:

' Article ' = = Array (self::has_one, ' article ', ' article_content_id '),


Inquire:

$result = Articlecontent::model (), find (' article_content_id =: aid ', Array (': aid ' = $articleId));
Prinr_r ($result-attributes); The fields of the Article_content table are all printed.
Print_r (attributes, article, $result); Get the fields of the article table
Print_r (user_name, UserInfo, article, $result); Get username



I am purely in order to construct the landlord said this kind of situation is written like this, generally should not be designed so, many are related fields are in that intermediate table, article, check the time is through the article model related to other tables query it
  • 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.