Class yii\redis\ActiveRecord
Inheritance | yii\redis\ActiveRecord » yii\db\BaseActiveRecord |
---|---|
Available since version | 2.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
Method | Description | Defined 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
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. |
---|
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 |
---|
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 yii\redis\ActiveQuery::where() on how to specify this parameter. |
return | integer | The number of rows deleted |
---|
public static yii\redis\ActiveQuery find ( ) | ||
return | yii\redis\ActiveQuery | The newly created yii\redis\ActiveQuery instance. |
---|
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. |
---|
public void insert ( $runValidation = true, $attributes = null ) | ||
$runValidation | ||
$attributes |
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 \yii\helpers\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 |
---|
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. |
---|
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 yii\redis\ActiveQuery::where() on how to specify this parameter. |
return | integer | The number of rows updated |
---|
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 yii\redis\ActiveQuery::where() on how to specify this parameter. |
return | integer | The number of rows updated |
---|