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 ...