|  | 
|  | 1 | +ActiveRecord orqali foydalanish | 
|  | 2 | +====================== | 
|  | 3 | + | 
|  | 4 | +Avvalo Yii2'da ActiveRecord'dan qanday foydalanish haqida umumiy ma'lumot olish uchun [qo'llanma](https://github.com/yiisoft/yii2/blob/master/docs/guide/db-active-record.md) ga qarang. | 
|  | 5 | + | 
|  | 6 | +Redis'dan ActiveRecord orqali foydalanish uchun sizning sinfingiz [[yii\redis\ActiveRecord]] vorisi bo'lish va  | 
|  | 7 | +sinfingizda kamida `attributes()` metodi bo'lishi kerak. | 
|  | 8 | + | 
|  | 9 | + | 
|  | 10 | + | 
|  | 11 | +PrimaryKey'ni `[[yii\redis\ActiveRecord::primaryKey()]]` orqali tanitish mumkin, agar berilmagan bo'lsa, `id` PrimaryKey sifatida tanitiladi. | 
|  | 12 | +PrimaryKey'ni ko'rsatmagan bo'lsangiz, PrimaryKey sifatida `id` olinganligini tekshiring. | 
|  | 13 | + | 
|  | 14 | +Quyida `Customer` nomli namunaviy model keltirilgan: | 
|  | 15 | + | 
|  | 16 | +```php | 
|  | 17 | +class Customer extends \yii\redis\ActiveRecord | 
|  | 18 | +{ | 
|  | 19 | + /** | 
|  | 20 | + * @return array the list of attributes for this record | 
|  | 21 | + */ | 
|  | 22 | + public function attributes() | 
|  | 23 | + { | 
|  | 24 | + return ['id', 'name', 'address', 'registration_date']; | 
|  | 25 | + } | 
|  | 26 | + | 
|  | 27 | + /** | 
|  | 28 | + * @return ActiveQuery defines a relation to the Order record (can be in other database, e.g. elasticsearch or sql) | 
|  | 29 | + */ | 
|  | 30 | + public function getOrders() | 
|  | 31 | + { | 
|  | 32 | + return $this->hasMany(Order::className(), ['customer_id' => 'id']); | 
|  | 33 | + } | 
|  | 34 | + | 
|  | 35 | + public static function find() | 
|  | 36 | + { | 
|  | 37 | + return new CustomerQuery(get_called_class()); | 
|  | 38 | + } | 
|  | 39 | +} | 
|  | 40 | + | 
|  | 41 | +class CustomerQuery extends \yii\redis\ActiveQuery | 
|  | 42 | +{ | 
|  | 43 | + /** | 
|  | 44 | + * Defines a scope that modifies the `$query` to return only active(status = 1) customers | 
|  | 45 | + */ | 
|  | 46 | + public function active() | 
|  | 47 | + { | 
|  | 48 | + return $this->andWhere(['status' => 1]); | 
|  | 49 | + } | 
|  | 50 | +} | 
|  | 51 | +``` | 
|  | 52 | + | 
|  | 53 | +Общее использование redis ActiveRecord очень похоже на БД ActiveRecord как описано в [руководстве](https://github.com/yiisoft/yii2/blob/master/docs/guide/db-active-record.md). | 
|  | 54 | +Он поддерживает тот же интерфейс и функции, за исключением следующих ограничений: | 
|  | 55 | + | 
|  | 56 | +Redis'dan ActiveRecord'da foydalanish, ma'lumotlar bazasi bilan ishlashga juda o'xshaydi. | 
|  | 57 | +Faqat u quyidagi cheklovlardan tashqari bir xil interfeys va xususiyatlarni qo'llab-quvvatlaydi: | 
|  | 58 | + | 
|  | 59 | +- Redis SQL'ni qo'llab-quvvatlamagani uchun quidagi metodlardan foydalanib bo'lmaydi | 
|  | 60 | + `where()`, `limit()`, `offset()`, `orderBy()` va `indexBy()`. | 
|  | 61 | +- `via` - jadval via orqali aniqlab (bog'lanib) bo'lmaydi, chunki redisda jadvallar mavjud emas. Siz bog'lanishlarni boshqa ma'lumotlar orqali belgilashingiz mumkin. | 
|  | 62 | + | 
|  | 63 | +Redis ActiveRecords dan oddiy ActiveRecord sinflariga va aksincha bog'lanishlarni aniqlashingiz mumkin. | 
|  | 64 | + | 
|  | 65 | +Masalan: | 
|  | 66 | + | 
|  | 67 | +```php | 
|  | 68 | +$customer = new Customer(); | 
|  | 69 | +$customer->attributes = ['name' => 'test']; | 
|  | 70 | +$customer->save(); | 
|  | 71 | +echo $customer->id; // id will automatically be incremented if not set explicitly | 
|  | 72 | + | 
|  | 73 | +$customer = Customer::find()->where(['name' => 'test'])->one(); // find by query | 
|  | 74 | +$customer = Customer::find()->active()->all(); // find all by query (using the `active` scope) | 
|  | 75 | +``` | 
0 commit comments