Yii Framework Forum: Yii-Active-Collection-Decorator - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii-Active-Collection-Decorator Rate Topic: -----

#1 User is offline   once 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 27-November 12

Posted 10 January 2014 - 03:35 PM

yii-active-collection-decorator

This extension can decorate any ActiveRecord method which returns list of the ActiveRecord models and you can use collection as a single ActiveRecord model.


##Requirements


Yii 1.*


##Installation


  • using git: go to the application.ext directory and clone project
    $> git clone git@github.com:oncesk/yii-active-collection-decorator.git
    

  • using archive: download archive and unpack it into application.ext directory, [download extension](https://github.com/o...hive/master.zip)



##Yii configuration


You can add extension into import section into your config/main.php file for class autoloading

return array(

  ...

  'import' => array(
    'ext.yii-active-collection-decorator.*'
  ),

  ...
);



##Usage


Override method which should return list of the ActiveRecord models such as findAll and etc..

Into your ActiveRecord model

/**
 * @property integer $id
 * @property integer $status
 * @property string $name
 * @property string $avatar
 */
class User extends CActiveRecord {

  const STATUS_ACTIVE = 1;
  const STATUS_DELETED = 2;

  ...

  public function findAll($condition = '', $params = array()) {
    return ActiveCollectionDecorator::createCollection($this, parent::findAll($condition, $params));
  }

  ...

  public function relations() {
    return array(
            'posts' => array(self::HAS_MANY, 'Post', 'user_id'),
        );
  }
}



**And now you can**

$userCollection = User::model()->findAll();

//  simple foreach
foreach ($userCollection as $user) {
  //  $user is a User model object
}

//  ArrayAccess interface
echo $userCollection[0]->id; //  get id of the first User model in the list of the models

//  fetch any attribute as array
print_r($userCollection->name); //  output: array(0 => 'John Smith', 1 => 'Sara Mitchel', ...)

//  you can set attribute value for all models
$userCollection->status = User::STATUS_DELETED;

//  you can save all models
$userCollection->save();

//  deletion
$userCollection->delete();  // all users will be deleted

//  you can get relations
print_r($userCollection->posts);  // in output you can see posts of every user

//  filter models
$newFilteredCollection = $userCollection->filter(function (User $user) {
    return $user->status == User::STATUS_ACTIVE;
}); // will be returned new collection which contains only models with active users

//  get attribute names
print_r($userCollection->attributeNames());

//  get relations
print_r($userCollection->relations());

//  call any method in the model
$stats = $userColletion->getStatistics(); // $stats - array of each model result

//  get first model, if elements count == 0 will be returned null
if ($first = $userCollection->first()) {
    echo $first->name;
}

//  get last model, if elements count == 0 will be returned null
if ($last = $userCollection->last()) {
    echo $last->name;
}



##Resources


* Github page https://github.com/o...ction-decorator
1

#2 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 11 January 2014 - 05:56 AM

I like the idea and the implementation looks so nice and tidy. I have a feeling this could be incorporated in the core framework, but I'd give it more thought.

BTW it's probably the best practical example of using CMap I've seen to date.
Don't be a dick.
0

#3 User is offline   once 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 27-November 12

Posted 13 January 2014 - 05:28 AM

it would be great =)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users