Statistical functions in Yii-relations data Association-PHP Tutorial-php Tutorial

Source: Internet
Author: User
The statistical function in the Yii-relations Data Association. Associated query. Yii also supports the so-called statistical query (or aggregate query ). It refers to the retrieval of the aggregated information of associated objects, such as the number of comments for each post and the average level of associated queries for each product. 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.
[Html]
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 )'),
);
}
}

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.
$ 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.
$ 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.
[Html]
Class User extends CActiveRecord
{
Public function relations ()
{
Return array (
'Posts' => array (self: has_attributes, 'post', 'author _ id', 'with' => 'comments: approved '),
);
}
}

Class User extends CActiveRecord
{
Public function relations ()
{
Return array (
'Posts' => array (self: has_attributes, 'post', 'author _ id', 'with' => 'comments: approved '),
);
}
}

Also supports statistical queries (or aggregate queries ). It refers to the retrieval of the aggregated information of associated objects, such as the number of comments for each post and the average rating of each product...

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.