Yii2 ListView does not iterate over results

My code:

Generating data data provider


 $result = Yii::$app->db->createCommand($sql)->queryAll();

 $data_provider = new \yii\data\ArrayDataProvider([

     'allModels'  => $result,

     'key' => 'part_id',

     'pagination' => [

        'pageSize' => 15,

     ],

 ]);

View:


ListView::widget([

    'dataProvider' => $data_provider,

    'layout' => '{items}',

    'options'      => [

        'class' => 'list-view',

        'id'    => 'search_results'

    ],

    'itemView'     => '_result',

]);

And _result :


 DetailView::widget([

   'model'      => $model,

   'attributes' => [

      'part_id',

   ],

 ])

Result:


Part Id 1

Part Id 1 

Part Id 1

[...]

Why???

If I do var_dump($data_provider) in View I get:


 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)

           [...]

       [...]

What am I doing wrong?

I`m pretty sure that DetailView is designed to show only one data row, not to itterate over a number of them.

In your case your List View is looping, and when It`s calling the DetailView with:


'itemView'     => '_result',

then DetailView is fetching only first data it contains. Then number of:

is matching number of all records that you are getting through:

??

Yes, this is precisely what happens… but I dont quite understand why ListView always sends same stuff to DetailView? I mean I would imagine this is the whole point of the list to have a ‘list’ of things… different things which then I can show…

I would imagine that $model should be different in every loop, shouldn’t it? If not then what is the point of looping at all?

$data_provider->allModels is an array not yet sorted by the key.

Probably you are getting redundant rows by your sql.

Try var_dump $data_provider->models instead of $data_provider->allModels, with $data_provider->pagination set to false.

[EDIT]

And I would like to see the sql.