Class yii\redis\ActiveRecord

Inheritanceyii\redis\ActiveRecord » yii\db\BaseActiveRecord
Available since version2.0

ActiveRecord is the base class for classes representing relational data in terms of objects.

This class implements the ActiveRecord pattern for the redis key-value store.

For defining a record a subclass should at least implement the attributes() method to define attributes. A primary key can be defined via primaryKey() which defaults to id if not specified.

The following is an example model called Customer:

class Customer extends \yii\redis\ActiveRecord
{
    public function attributes()
    {
        return ['id', 'name', 'address', 'registration_date'];
    }
}

Public Methods

Hide inherited methods

MethodDescriptionDefined By
attributes() Returns the list of all attribute names of the model. yii\redis\ActiveRecord
buildKey() Builds a normalized key from a given primary key value. yii\redis\ActiveRecord
deleteAll() Deletes rows in the table using the provided conditions. yii\redis\ActiveRecord
find() yii\redis\ActiveRecord
getDb() Returns the database connection used by this AR class. yii\redis\ActiveRecord
insert() yii\redis\ActiveRecord
keyPrefix() Declares prefix of the key that represents the keys that store this records in redis. yii\redis\ActiveRecord
primaryKey() Returns the primary key name(s) for this AR class. yii\redis\ActiveRecord
updateAll() Updates the whole table using the provided attribute values and conditions. yii\redis\ActiveRecord
updateAllCounters() Updates the whole table using the provided counter changes and conditions. yii\redis\ActiveRecord

Method Details

attributes() public method

Returns the list of all attribute names of the model.

This method must be overridden by child classes to define available attributes.

public array attributes ( )
return array

List of attribute names.

buildKey() public static method

Builds a normalized key from a given primary key value.

public static string buildKey ( $key )
$key mixed

The key to be normalized

return string

The generated key

deleteAll() public static method

Deletes rows in the table using the provided conditions.

WARNING: If you do not specify any condition, this method will delete ALL rows in the table.

For example, to delete all customers whose status is 3:

Customer::deleteAll(['status' => 3]);
public static integer deleteAll ( $condition null )
$condition array

The conditions that will be put in the WHERE part of the DELETE SQL. Please refer to ActiveQuery::where() on how to specify this parameter.

return integer

The number of rows deleted

find() public static method

public static yii\redis\ActiveQuery find ( )
return yii\redis\ActiveQuery

The newly created yii\redis\ActiveQuery instance.

getDb() public static method

Returns the database connection used by this AR class.

By default, the "redis" application component is used as the database connection. You may override this method if you want to use a different database connection.

public static yii\redis\Connection getDb ( )
return yii\redis\Connection

The database connection used by this AR class.

insert() public method

public void insert ( $runValidation true, $attributes null )
$runValidation
$attributes
keyPrefix() public static method

Declares prefix of the key that represents the keys that store this records in redis.

By default this method returns the class name as the table name by calling Inflector::camel2id(). For example, 'Customer' becomes 'customer', and 'OrderItem' becomes 'order_item'. You may override this method if you want different key naming.

public static string keyPrefix ( )
return string

The prefix to apply to all AR keys

primaryKey() public static method

Returns the primary key name(s) for this AR class.

This method should be overridden by child classes to define the primary key.

Note that an array should be returned even when it is a single primary key.

public static string[] primaryKey ( )
return string[]

The primary keys of this record.

updateAll() public static method

Updates the whole table using the provided attribute values and conditions.

For example, to change the status to be 1 for all customers whose status is 2:

Customer::updateAll(['status' => 1], ['id' => 2]);
public static integer updateAll ( $attributes, $condition null )
$attributes array

Attribute values (name-value pairs) to be saved into the table

$condition array

The conditions that will be put in the WHERE part of the UPDATE SQL. Please refer to ActiveQuery::where() on how to specify this parameter.

return integer

The number of rows updated

updateAllCounters() public static method

Updates the whole table using the provided counter changes and conditions.

For example, to increment all customers' age by 1,

Customer::updateAllCounters(['age' => 1]);
public static integer updateAllCounters ( $counters, $condition null )
$counters array

The counters to be updated (attribute name => increment value). Use negative values if you want to decrement the counters.

$condition array

The conditions that will be put in the WHERE part of the UPDATE SQL. Please refer to ActiveQuery::where() on how to specify this parameter.

return integer

The number of rows updated