2017-03-17 33 views
0

我是新手,所以我希望你能幫助我。 我有yii2,我需要實現elasticSearch(ES)。 全部設置。 我做了標準模型和標準CRUD。就在幾個改變以實現ES樣。如何分配我的_id作爲主鍵

use yii\elasticsearch\ActiveRecord; 

但我有我的看法/ index.php文件 得到_id問題得到_id在view.php我做

$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我只能等環節W/O ID

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

請幫助SE

附: 這裏是一個的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.[email protected]' (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 '[email protected]' (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 

回答

0

可以set.good運氣

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