PHP code example of borales / yii2-elasticsearch-behavior
1. Go to this page and download the library: Download borales/yii2-elasticsearch-behavior library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
borales / yii2-elasticsearch-behavior example snippets
class Post extends \yii\db\ActiveRecord
{
public function behaviors()
{
return [
...
'elasticsearch' => [
'class' => \borales\behaviors\elasticsearch\Behavior::className(),
'mode' => 'command',
'elasticIndex' => 'project-index',
'elasticType' => 'posts',
'dataMap' => [
'id' => 'id',
'title' => 'name',
'body' => function() {
return strip_tags($this->body);
},
'date_publish' => function() {
return date('U', strtotime($this->date_create));
},
'author' => function() {
return ucfirst($this->author->name);
}
]
],
];
}
...
}
class Post extends \yii\db\ActiveRecord
{
public function behaviors()
{
return [
...
'elasticsearch' => [
'class' => \borales\behaviors\elasticsearch\Behavior::className(),
'mode' => 'model',
'elasticClass' => \common\models\elastic\PostElastic::className(),
'dataMap' => [
'id' => 'id',
'title' => 'name',
'body' => function() {
return strip_tags($this->body);
},
'date_publish' => function() {
return date('U', strtotime($this->date_create));
},
'author' => function() {
return ucfirst($this->author->name);
}
]
],
];
}
...
}
...
class PostElastic extends \yii\elasticsearch\ActiveRecord
{
/**
* @return array the list of attributes for this record
*/
public function attributes()
{
// path mapping for '_id' is setup to field 'id'
return ['id', 'title', 'body', 'date_publish', 'author'];
}
}
bash
$ php composer.phar