From 8792f21f57139c30ba94fa3974deebe4b2b9af93 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Mon, 25 Nov 2013 03:33:37 +0100 Subject: [PATCH] more docs [ci skip] --- extensions/elasticsearch/README.md | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/extensions/elasticsearch/README.md b/extensions/elasticsearch/README.md index 7988de1..bc272d1 100644 --- a/extensions/elasticsearch/README.md +++ b/extensions/elasticsearch/README.md @@ -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. \ No newline at end of file +- 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) +``` \ No newline at end of file