|
|
|
@ -67,10 +67,29 @@ The following is an example model called `Customer`:
|
|
|
|
|
```php |
|
|
|
|
class Customer extends \yii\elasticsearch\ActiveRecord |
|
|
|
|
{ |
|
|
|
|
/** |
|
|
|
|
* @return array the list of attributes for this record |
|
|
|
|
*/ |
|
|
|
|
public function attributes() |
|
|
|
|
{ |
|
|
|
|
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)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
@ -89,4 +108,20 @@ It supports the same interface and features except the following limitations and
|
|
|
|
|
- `via`-relations can not be defined via a table as there are not tables in elasticsearch. You can only define relations via other records. |
|
|
|
|
- As elasticsearch is a data storage and search engine there is of course support added for search your records. |
|
|
|
|
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) |
|
|
|
|
``` |