Yii2,elasticsearch。如何将我的_id指定为primaryKey

时间:2017-03-17 22:32:16

标签: elasticsearch yii2

我是新手,所以我希望你能帮助我。 我有yii2,我需要实现elasticSearch(ES)。 全部设置。 我制作了标准模型和标准CRUD。只需进行一些改变即可实现ES

use yii\elasticsearch\ActiveRecord;

但是我在view / index.php中获取_id有问题 在view.php中获取_id我做了

$model->_id = $model->primaryKey;

但我如何在index.php中使用DataProvider修复它?

我的属性

public function attributes()
{
    return [
        '_id',
        'first_name',
        'last_name',
        'email',
        'created',
        'brand',
        'product',
        'qty',
        'sum'
    ];
}

我的规则

public function rules()
{
    return [
        [['first_name', 'last_name', 'email', 'created',
            'brand', 'product', 'qty', 'sum'
        ],
            'safe'],
        [['qty'], 'integer'],
        [['sum'], 'double'],
        ['email', 'email'],
    ];
}

我认为它应该像MongoDB一样工作,但事实并非如此。

如果我想删除或查看或更新index.php上的数据我只能这样的链接没有id

http://yii2.dev/index.php?r=elastic%2Fdelete&id=

请帮忙

P.S。 这是一个var_dump

array (size=5)
  0 => 
    object(frontend\models\Elastic)[67]
      private '_id' (yii\elasticsearch\ActiveRecord) => string 'AVreA6SUvBgWLKxTzUZn' (length=20)
  private '_score' (yii\elasticsearch\ActiveRecord) => float 1
  private '_version' (yii\elasticsearch\ActiveRecord) => null
  private '_highlight' (yii\elasticsearch\ActiveRecord) => null
  private '_attributes' (yii\db\BaseActiveRecord) => 
    array (size=8)
      'created' => string '2017-03-12' (length=10)
      'first_name' => string 'Luka' (length=4)
      'last_name' => string 'Signori' (length=7)
      'email' => string 'l.signori@gmail.com' (length=19)
      'brand' => string 'Apple' (length=5)
      'product' => string 'iPad' (length=4)
      'qty' => string '35' (length=2)
      'sum' => string '600.00' (length=6)
  private '_oldAttributes' (yii\db\BaseActiveRecord) => 
    array (size=8)
      'created' => string '2017-03-12' (length=10)
      'first_name' => string 'Luka' (length=4)
      'last_name' => string 'Signori' (length=7)
      'email' => string 'l.signori@gmail.com' (length=19)
      'brand' => string 'Apple' (length=5)
      'product' => string 'iPad' (length=4)
      'qty' => string '35' (length=2)
      'sum' => string '600.00' (length=6)
  private '_related' (yii\db\BaseActiveRecord) => 
    array (size=0)
      empty
  private '_errors' (yii\base\Model) => null
  private '_validators' (yii\base\Model) => null
  private '_scenario' (yii\base\Model) => string 'default' (length=7)
  private '_events' (yii\base\Component) => 
    array (size=0)
      empty
  private '_behaviors' (yii\base\Component) => 
    array (size=0)
      empty

1 个答案:

答案 0 :(得分:0)

你可以设置运气

$dataProvider = new ActiveDataProvider([
       'query' => $query,
       'key' => ['your primaryKey']
       'sort' => [
          'defaultOrder' => ['datetime'=>SORT_DESC]
     ],
]);