Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Yii2 Redis Laravel Package

yiisoft/yii2-redis

Yii2 Redis extension providing Redis-backed cache, mutex, and session handlers plus an ActiveRecord layer for storing and querying structured data in Redis. Requires Redis 2.6.12+ and PHP 7.4+ (best on PHP 8).

View on GitHub
Deep Wiki
Context7

Usando o ActiveRecord

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:

  • Com os redis não suporta SQL, a API de consulta está limitada aos seguintes métodos: where(), limit(), offset(), orderBy() and indexBy().
  • Relações não podem ser definidas através de uma tabela, pois não há tabelas em redis. Você só pode definir relações através de outros registros.

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)
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity