yiisoft/yii2-redis
Yii2 Redis extension providing Redis connection plus Cache, Session, and Mutex handlers, and an ActiveRecord layer to store and query structured data in Redis using familiar Yii2 patterns. Requires Redis 2.6.12+ and PHP 7.4+ (best on PHP 8).
Para obter informações gerais sobre como usar o ActiveRecord do yii, consulte o guia.
Para definir uma classe redis ActiveRecord, sua classe de registro precisa ser estendida de [[yii\redis\ActiveRecord]] e
implementar pelo menos o método attributes () para definir os atributos do registro
Uma chave primária pode ser definida via [[yii\redis\ActiveRecord::primaryKey()]] cujo valor padrão é id se não for especificado.
A chave primária precisa fazer parte dos atributos, então tenha certeza que você tem um atributo id definido se você não especificar sua própria chave primária.
A seguir um exemplo de modelo chamado Customer:
class Customer extends \yii\redis\ActiveRecord
{
/**
* [@return](https://github.com/return) array the list of attributes for this record
*/
public function attributes()
{
return ['id', 'name', 'address', 'registration_date'];
}
/**
* [@return](https://github.com/return) ActiveQuery defines a relation to the Order record (can be in other database, e.g. elasticsearch or sql)
*/
public function getOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id']);
}
public static function find()
{
return new CustomerQuery(get_called_class());
}
}
class CustomerQuery extends \yii\redis\ActiveQuery
{
/**
* Defines a scope that modifies the `$query` to return only active(status = 1) customers
*/
public function active()
{
return $this->andWhere(['status' => 1]);
}
}
O uso geral de redis ActiveRecord é muito semelhante ao banco de dados ActiveRecord, conforme descrito no guia. Ele suporta a mesma interface e recursos, exceto as seguintes limitações:
where(), limit(), offset(), orderBy() and indexBy().Também é possível definir relações de redis ActiveRecords para classes normais de ActiveRecord e vice-versa.
Exemplo de uso:
$customer = new Customer();
$customer->attributes = ['name' => 'test'];
$customer->save();
echo $customer->id; // id will automatically be incremented if not set explicitly
$customer = Customer::find()->where(['name' => 'test'])->one(); // find by query
$customer = Customer::find()->active()->all(); // find all by query (using the `active` scope)
How can I help you explore Laravel packages today?