yiisoft/yii2 — pull request #905 by cebe (2013-09-25 10:07:39+00:00)
Title: [WIP] Redis ActiveRecord

This is WIP redis ActiveRecord support.
It has the following features:
- insert, update, delete interface is exactly the same as db\ActiveRecord
- relation, relations via (except relations viaTable because there are no tables in a key value store ;) )
- optimistic locking
- find() has the same interface as db\ActiveRecord except a few limitations. It does not support select(), from() join(), group(), having(), union()
- complex where conditions via Lua Script which is executed on redis server and will return matching records.

TODO
- support for orderBy()
- support LIKE conditions

A redis AR can be created like this:

```php
class Customer extends yii\redis\ActiveRecord
{
// relation declaration is the same as in db\AR
/**
* @return \yii\redis\ActiveRelation
*/
public function getOrders()
{
return $this->hasMany('Order', array('customer_id' => 'id'));
}

// also scopes work the same as in db\AR but with limited query options (where is fully supported)
public static function active($query)
{
$query->andWhere(array('status' => 1));
}
// this is the only new thing. instead of tableName() you implement
// getRecordSchema() to define your data schema
// as there is no schema in redis you define it here. RecordSchema extends from db\TableSchema
public static function getRecordSchema()
{
return new RecordSchema(array(
'name' => 'customer', // the key prefix for storing data (just like tablename in db)
'primaryKey' => array('id'), // the primary key (can be composite)
'columns' => array( // columns (AR attributes)
'id' => 'integer',
'email' => 'string',
'name' => 'string',
'address' => 'string',
'status' => 'integer'
)
));
}
}
```