Load $items from DB in Kartik's SortableInput

Hello everyone:

First of all I don’t know if posting this here is correct, if not please move it.

Here I go:

I can’t figure out how to load the items in a SortableInput from the database.

How is the right way to do it? Do I have to make a query in the model and pass it to $items in the widget?

I’m trying to use the example #5 in my _form.php SortableInput, load data from tableA in the rigth list, drag&drop to the left list (which starts empty) and save the elements dropped to tableB by id.

Here’s what I’ve got so far, but it loads only one element with a 0 displayed.




    <div class="row">

        <div class="col-sm-6">


        <?= $form->field($model, 'receta_id')->widget(SortableInput::classname(), [

            'name' => 'kv-conn-1',

            'items' => [],

            'hideInput' => false,

            'sortableOptions' => [

                'itemOptions' => ['class'=>'alert alert-warning'],

                'connected' => true,

                ],

            'options' => ['class' => 'form-control', 'readonly' => true]

        ]);

        ?>

        </div>

        <div class="col-sm-6">


        <?= $form->field($model, 'dosis_id')->widget(SortableInput::classname(), [

            'name' => 'kv-conn-2',

            'items' => $items= [ArrayHelper::map(Dosis::find()->all(),'id_dosis','cantidad')],

            'hideInput' => false,

            'sortableOptions' => [

                    'connected' => true,

                ],

            'options' => ['class' => 'form-control', 'readonly' => true]

            ]);

        ?>

        </div>        

    </div>



I know it looks like a trivial thing, but I’m only 2 weeks working with Yii2 and still don’t have the grasp of all the elements involved.

Thank you.

In case somebody need it, this is the solution:

First use this, define an $items array and fill it with a foreach on the tabel you need.




    <?php

        $items = [];

        $dosis = Dosis::find()->all();

        foreach ($dosis as $key) {

            $items[$key->id_dosis] = [

                'content' => $key->insumo->nombre_insumo,

                'options' => ['data' => ['id_dosis'=>$key->insumo->nombre_insumo]],

            ];

        }

    ?>



In my case I needed to fill the items with a field from a related table to the one whose form I was working on (where the above code is), so make sure you had created the relation and use it in the foreach:




                'content' => $key->insumo->nombre_insumo,

                'options' => ['data' => ['id_dosis'=>$key->insumo->nombre_insumo]],