Detailed usage of the relations data association query and statistics function in Yii, yiirelations

Source: Internet
Author: User

Detailed usage of the relations data association query and statistics function in Yii, yiirelations

This article describes how to use the relations data association query and statistics function in Yii. We will share this with you for your reference. The details are as follows:

Associated query. Yii also supports the so-called statistical query (or aggregate query ). It refers to retrieving the aggregated information of associated objects, such as the number of comments for each post and the average grade of each product. The statistical query is executed only by HAS_MANY (for example, a post has many comments) or MANY_MANY (for example, a post belongs to many categories and a category has many posts) associated objects.

The execution of statistical queries is very similar to the association query described earlier. We first need to declare a statistical query in the relations () method of CActiveRecord.

class Post extends CActiveRecord{  public function relations()  {    return array(      'commentCount'=>array(self::STAT, 'Comment', 'post_id'),      'categoryCount'=>array(self::STAT, 'Category', 'post_category(post_id,category_id)'),    );  }}

Join query namespace

Join queries can also be executed with namespaces. There are two forms. In the first form, the namespace is applied to the master model. Second, the namespace is applied to the associated model.

The following code shows how to apply a namespace to the master model.
Copy codeThe Code is as follows: $ posts = Post: model ()-> published ()-> recently ()-> with ('comments')-> findAll ();
This is very similar to non-associated queries. The only difference is that we use with () to call the namespace. This query should return the most recent post and their comments.

The following code shows how to apply a namespace to an associated model.
Copy codeThe Code is as follows: $ posts = Post: model ()-> with ('comments: recently: approved')-> findAll ();
The above query will return all post and Their reviewed comments. Note that comments refers to the association name, while recently and approved refer to the namespace declared in the Comment model class. The associated names and namespaces should be separated by colons.

The namespace can also be specified in the with option of the association rule declared in CActiveRecord: relations. In the following example, if we access $ user-> posts, it will return all post-review comments for this post.

class User extends CActiveRecord{  public function relations()  {    return array(      'posts'=>array(self::HAS_MANY, 'Post', 'author_id', 'with'=>'comments:approved'),    );  }}

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.