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