Avvalo Yii2'da ActiveRecord'dan qanday foydalanish haqida umumiy ma'lumot olish uchun qo'llanma ga qarang.
Redis'dan ActiveRecord orqali foydalanish uchun sizning sinfingiz yii\redis\ActiveRecord vorisi bo'lish va
sinfingizda kamida attributes() metodi bo'lishi kerak.
PrimaryKey'ni [[yii\redis\ActiveRecord::primaryKey()]] orqali tanitish mumkin, agar berilmagan bo'lsa, id PrimaryKey sifatida tanitiladi.
PrimaryKey'ni ko'rsatmagan bo'lsangiz, PrimaryKey sifatida id olinganligini tekshiring.
Quyida Customer nomli namunaviy model keltirilgan:
class Customer extends \yii\redis\ActiveRecord
{
/**
* @return array the list of attributes for this record
*/
public function attributes()
{
return ['id', 'name', 'address', 'registration_date'];
}
/**
* @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]);
}
}
ΠΠ±ΡΠ΅Π΅ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅ redis ActiveRecord ΠΎΡΠ΅Π½Ρ ΠΏΠΎΡ ΠΎΠΆΠ΅ Π½Π° ΠΠ ActiveRecord ΠΊΠ°ΠΊ ΠΎΠΏΠΈΡΠ°Π½ΠΎ Π² ΡΡΠΊΠΎΠ²ΠΎΠ΄ΡΡΠ²Π΅. ΠΠ½ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅Ρ ΡΠΎΡ ΠΆΠ΅ ΠΈΠ½ΡΠ΅ΡΡΠ΅ΠΉΡ ΠΈ ΡΡΠ½ΠΊΡΠΈΠΈ, Π·Π° ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ΠΌ ΡΠ»Π΅Π΄ΡΡΡΠΈΡ ΠΎΠ³ΡΠ°Π½ΠΈΡΠ΅Π½ΠΈΠΉ:
Redis'dan ActiveRecord'da foydalanish, ma'lumotlar bazasi bilan ishlashga juda o'xshaydi. Faqat u quyidagi cheklovlardan tashqari bir xil interfeys va xususiyatlarni qo'llab-quvvatlaydi:
where(), limit(), offset(), orderBy() va indexBy().via - jadval via orqali aniqlab (bog'lanib) bo'lmaydi, chunki redisda jadvallar mavjud emas. Siz bog'lanishlarni boshqa ma'lumotlar orqali belgilashingiz mumkin.Redis ActiveRecords dan oddiy ActiveRecord sinflariga va aksincha bog'lanishlarni aniqlashingiz mumkin.
Masalan:
$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)