|
|
@ -67,10 +67,29 @@ The following is an example model called `Customer`: |
|
|
|
```php |
|
|
|
```php |
|
|
|
class Customer extends \yii\elasticsearch\ActiveRecord |
|
|
|
class Customer extends \yii\elasticsearch\ActiveRecord |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @return array the list of attributes for this record |
|
|
|
|
|
|
|
*/ |
|
|
|
public function attributes() |
|
|
|
public function attributes() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return ['id', 'name', 'address', 'registration_date']; |
|
|
|
return ['id', 'name', 'address', 'registration_date']; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @return ActiveRelation defines a relation to the Order record (can be in other database, e.g. redis or sql) |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function getOrders() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('id'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Defines a scope that modifies the `$query` to return only active(status = 1) customers |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static function active($query) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$query->andWhere(array('status' => 1)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
``` |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
@ -90,3 +109,19 @@ It supports the same interface and features except the following limitations and |
|
|
|
- As elasticsearch is a data storage and search engine there is of course support added for search your records. |
|
|
|
- As elasticsearch is a data storage and search engine there is of course support added for search your records. |
|
|
|
TBD ... |
|
|
|
TBD ... |
|
|
|
- It is also possible to define relations from elasticsearch ActiveRecords to normal ActiveRecord classes and vice versa. |
|
|
|
- It is also possible to define relations from elasticsearch ActiveRecords to normal ActiveRecord classes and vice versa. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Elasticsearch separates primary key from attributes. You need to set the `id` property of the record to set its primary key. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Usage example: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
|
|
|
$customer = new Customer(); |
|
|
|
|
|
|
|
$customer->id = 1; |
|
|
|
|
|
|
|
$customer->attributes = ['name' => 'test']; |
|
|
|
|
|
|
|
$customer->save(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$customer = Customer::get(1); // get a record by pk |
|
|
|
|
|
|
|
$customers = Customer::get([1,2,3]); // get a records multiple by pk |
|
|
|
|
|
|
|
$customer = Customer::find()->where(['name' => 'test'])->one(); // find by query |
|
|
|
|
|
|
|
$customer = Customer::find()->active()->all(); // find all by query (using the `active` scope) |
|
|
|
|
|
|
|
``` |