Yii-relations statistical functions in data Association _php tutorial

Source: Internet
Author: User
Associated queries, YII also supports so-called statistical queries (or aggregate queries). It refers to the aggregation information that retrieves the associated object, such as the number of comments per post, the average rating for each product, and so on. Statistical queries are executed only by Has_many (for example, a post has a lot of comments) or many_many (for example, a post belongs to many categories and a category has many post) associated objects.
The execution statistics query is very similar to the associated query described earlier. We first need to declare the statistical query in the Cactiverecord relations () method.
[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) '),
);
}
Association query namespace
The associated query can also be executed with the namespace. There are two kinds of forms. In the first form, namespaces are applied to the main model. In the second form, namespaces are applied to the association model.
The following code shows how to apply a namespace to the main model.
$posts =post::model ()->published ()->recently ()->with (' comments ')->findall ();
This is very similar to a non-associative query. The only difference is that we used a with () call after the namespace. This query should return the most recently published post and their comments.
The following code shows how to apply a namespace to an association 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 association names, while recently and approved refer to namespaces declared in the Comment model class. Association 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 visit $user->posts, it will return all post-audit comments for this post.
[HTML]
Class User extends Cactiverecord
{
Public Function relations ()
{
Return Array (
' Posts ' =>array (self::has_many, ' Post ', ' author_id ', ' with ' = ' comments:approved '),
);
}
}

Class User extends Cactiverecord
{
Public Function relations ()
{
Return Array (
' Posts ' =>array (self::has_many, ' Post ', ' author_id ', ' with ' = ' comments:approved '),
);
}
}

http://www.bkjia.com/PHPjc/477598.html www.bkjia.com true http://www.bkjia.com/PHPjc/477598.html techarticle associated queries, YII also supports so-called statistical queries (or aggregate queries). It refers to the aggregation information that retrieves the associated object, such as the number of comments per post, the average rating for 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.