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

活动记录的使用

对于如何使用 yii 的活动记录一般信息请参阅 指南

定义一个 redis 活动记录类,你的记录类需要继承自 [[yii\redis\ActiveRecord]] 并且 至少实现 attributes() 方法来定义记录的属性。 一个没有指定默认值,默认为 id 的主键可以通过 [[yii\redis\ActiveRecord::primaryKey()]] 定义。 主键是属性中必要的一部分,所以请确保你有一个 id 属性定义的, 如果你没有指定自己的主键。

以下是一个 Customer 的实例模型:

class Customer extends \yii\redis\ActiveRecord
{
    /**
     * [@return](https://github.com/return) array 此记录的属性列表
     */
    public function attributes()
    {
        return ['id', 'name', 'address', 'registration_date'];
    }

    /**
     * [@return](https://github.com/return) ActiveQuery 定义一个关联到 Order 的记录(可以在其它数据库中,例如 elasticsearch 或者 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
{
    /**
     * 定义一个修改 `$query` 的范围返回有效(status = 1)的客户。
     */
    public function active()
    {
        return $this->andWhere(['status' => 1]);
    }
}

redis 活动记录的一般用法和数据库活动记录非常相似,正如 指南 中所描述的。 它支持相同的界面和功能,除了以下限制:

  • 由于 redis 不支持 SQL 查询,因此查询 API 仅限于以下方法: where()limit()offset()orderBy()indexBy()。 (orderBy() 尚未实现:#1305)
  • 由于 redis 中没有表,因此无法通过 via 表来定义关系。你只能通过其他记录来定义关系。

另外,也可以定义从 redis 的活动记录到普通的活动记录的关系,反之亦然。

使用实例:

$customer = new Customer();
$customer->attributes = ['name' => 'test'];
$customer->save();
echo $customer->id; // 如果没有明确设置 id 会自动递增

$customer = Customer::find()->where(['name' => 'test'])->one(); // 通过 query 查找
$customer = Customer::find()->active()->all(); // 通过 query 查找全部(使用 `active` 范围)
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