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() メソッドを実装する必要があります。 プライマリ・キーは [[yii\redis\ActiveRecord::primaryKey()]] によって定義することが出来ますが、指定されない場合のデフォルト値は id となります。 この 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 レコードに対するリレーションを定義 (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
{
    /**
     * アクティブ (status = 1) である顧客だけを返すように `$query` を修正するスコープを定義
     */
    public function active()
    {
        return $this->andWhere(['status' => 1]);
    }
}

redis のアクティブレコードの一般的な使用方法は、ガイド で説明されているデータベースのアクティブレコードの場合と非常によく似ています。 次の制限を除けば、同じインタフェイスと機能をサポートしています。

  • redis は SQL をサポートしていないので、クエリの API は次のメソッドに限定されます。 すなわち、where()limit()offset()orderBy() および indexBy()
  • redis にはテーブルがないため、via リレーションはテーブルによって定義することはできません。他のレコードを通じてリレーションを定義することだけが出来ます。

redis のアクティブレコードから通常のアクティブレコードへのリレーションを定義することも、また、その逆も、可能です。

使用例:

$customer = new Customer();
$customer->attributes = ['name' => 'test'];
$customer->save();
echo $customer->id; // id は明示的にセットされない場合は自動的にインクリメントされる

$customer = Customer::find()->where(['name' => 'test'])->one(); // クエリによって検索
$customer = Customer::find()->active()->all(); // クエリ (`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