Yii 1.1: yii-auto-stat-relation

Enhanced ActiveRecord class, that automatically adds STAT relations for each HAS_MANY and MANY_MANY
no followers

This extension allows you to get count of HAS_MANY or MANY_MANY related objects without using slow operations like count($model->relationName) or manually adding STAT relation to the relations() array.

It could help, when you have lots of models with complex relations, that changes from time to time, and you don't want to change STAT relations every time, when your HAS_MANY or MANY_MANY relation is changed. When your project become stable, and DB structure is 'freezed', you can write STAT relations manually to improve perfomance for 0.5% :)


Yii 1.1 or above. No special requirements


To use it, just extend AutoStatActiveRecord class by your model

The extension is consists of just a single file - AutoStatActiveRecord.php, which have to be placed in some "imported" directory.

Your model class:

class User extends AutoStatActiveRecord
   // ...... stuff
   public function relations()
        return array(
            'posts' => array(self::HAS_MANY, 'Post', 'author_user_id'),
            'friends' => array(self::MANY_MANY, 'User', '{{user_user_link_table}}(user_id,friend_id)'),
    // ...... stuff

Example code with your model:

//Lets read user from DB:
$user = User::model()->findByPk(11);
//Counting related items with 'count' function (Just for example! Don't do it at home!)
$friends_bad_practice = count($user->friends);
$posts_bad_practive = count($user->posts);
//Counting related items with automatic generated relation:
$frients_good_practice = $user->friends_AUTOSTAT;
$posts_good_practice = $user->posts_AUTOSTAT;
//Counting related items with function call:
$friends_the_best_practice = $user->countRelation('friends');
$posts_the_best_practice = $user->countRelation('posts');


Project is placed on a github:

Be the first person to leave a comment

Please to leave your comment.

Create extension