widgetyii2

How to access the keys in array of items in Sortable-Input Yii2


I'm having a hard time trying to figure how to resolve this. I would like to know how can I access and use the keys in the items array, for use in a batchInsert?

Given the next scenario: I have a tableA like this

id | tableB_id | tableC_id

Where id_tableA is auto incremental, and both tableB_id and tableC_id are FK. I need to insert in tableA multiple id from tableB which where selected from a connected Sortable, and the same id from tableC, which was selected from a dropdown, resulting in something like this:

id | tableB_id | tableC_id
--------------------------
1  |     2     |    4
2  |     3     |    4
3  |     5     |    4
.  |     .     |    4
n  |     x     |    4

There’s no problem when I select only 1 item and submit the form, but when I select two or more I can’t submit because obviously the tableB_id can accept only integer.

Form image to clarify


Solution

  • How about changing your Dosis ID to a string, and explode(',', $dosisId) it to get an array of the ids?