Yii2: Introduce model collections

Created on 23 Feb 2017  路  14Comments  路  Source: yiisoft/yii2

Do we have this option on yii2? where a callback can be applied to collection of models through active Query to allow for modification of models before returning sample code

Model::find()->iterate(function($model)
{
    return model->attribute=modify(model->attribute)
})->all();

I understand one way to achive this is through afterFind, one drawback to this includes, its not triggered when you use asArray() and sometime you want to dynamically modify the attribute without alot ofif elsestatements in the afterFind()

db under discussion

Most helpful comment

I started experimenting with something that could become an official extension:
https://github.com/yiisoft/yii2-model-collection
here are some usage docs: https://github.com/yiisoft/yii2-model-collection/tree/master/docs/guide

It's an early draft so it may not even work. but I'd like to have your feedback on it.

All 14 comments

the is no support yet for advanced (object) array handling with the use of Collections

I have added a support am waiting for it to be approved

You can use array_map() for this.

@rob006 thanks array_map() can work, I think

Model::find()->iterate(function($model)
{
    return model->attribute=modify(model->attribute)
})->all();

provides a neater way of doing the job

If something like that is to be done, proper collections and populating models into collections should be introduced instead of hardcoding things into model itself.

I'm using for this cases the YaLinqo, it's good and have a good performance.

Something like this could (should) be used: https://github.com/nikic/iter

Basically this feature is not related to AR or models, but to iterators.

Alternatively if you do not want a dependency, you could introduce an IteratorHelper that exposes the functionality.
Ideally these things should be implemented lazily, to optimize cases where not every element is iterated over...

Related: #13921

how about just editing this line? https://github.com/yiisoft/yii2/blob/master/framework/db/Query.php#L229 to make it configurable to what it will use to store the data from the database

something like

public $collectionClass;

public function collection()
{
    if (null !== $this->collectionClass) {
        return new $this->collectionClass;
    }
    return [];
}

public function all()
{
    // ...
    $result = $this->collection();
    // ...
}

that way it can be configured per model or query

class Store extends ActiveRecord
{
     public static function find()
     {
          $query = parent::find();
          $query->collectionClass = StoreCollection::className();
          return $query;
     }
}

bc break is kept and if you want more specific behavior you can extend collection() method.

I started experimenting with something that could become an official extension:
https://github.com/yiisoft/yii2-model-collection
here are some usage docs: https://github.com/yiisoft/yii2-model-collection/tree/master/docs/guide

It's an early draft so it may not even work. but I'd like to have your feedback on it.

Looks nice! I'll add some comments.

This is look awesome

Closing this issue now, track of implementation details will be discussed at https://github.com/yiisoft/yii2-collection

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexraputa picture alexraputa  路  53Comments

SamMousa picture SamMousa  路  55Comments

samdark picture samdark  路  52Comments

sepidemahmoodi picture sepidemahmoodi  路  104Comments

rosancoderian picture rosancoderian  路  46Comments