Yii2 ListView不会迭代结果

时间:2015-11-08 14:49:13

标签: view iteration yii2 dataprovider

我的代码:

生成数据数据提供者

 $result = Yii::$app->db->createCommand($sql)->queryAll();
 $data_provider = new \yii\data\ArrayDataProvider([
     'allModels'  => $result,
     'key' => 'part_id',
     'pagination' => [
        'pageSize' => 15,
     ],
 ]);

查看:

ListView::widget([
    'dataProvider' => $data_provider,
    'layout' => '{items}',
    'options'      => [
        'class' => 'list-view',
        'id'    => 'search_results'
    ],
    'itemView'     => '_result',
]);

并且_result:

 DetailView::widget([
   'model'      => $model,
   'attributes' => [
      'part_id',
   ],
 ])

结果:

Part Id 1
Part Id 1 
Part Id 1
[...]

为什么???

如果我在View中得到var_dump($ data_provider):得到:

 object(yii\data\ArrayDataProvider)[105]
   public 'key' => string 'part_id' (length=7)
   public 'allModels' => 
     array (size=100)
       0 => 
         array (size=7)
           'part_id' => string '1' (length=1)
           [...]
       1 => 
         array (size=7)
           'part_id' => string '2' (length=1)
           [...]
       2 => 
         array (size=7)
           'part_id' => string '3' (length=1)
           [...]
       [...]

我做错了什么?

0 个答案:

没有答案