Skip to content

Instantly share code, notes, and snippets.

@gonimar
Last active August 29, 2015 13:56
Show Gist options
  • Save gonimar/8809623 to your computer and use it in GitHub Desktop.
Save gonimar/8809623 to your computer and use it in GitHub Desktop.
<?php
use yii\base\Model;
use yii\helpers\ArrayHelper;

    public function actionBatchUpdate()
    {
        $items = Item::find()->all();

        // Делаем ключом массива уникальное поле модели
        $items = ArrayHelper::index($items, 'id');

        if(Model::loadMultiple($items, $_POST) && Model::validateMultiple($items)) {
            foreach ($items as $item) {
                $item->save(false);
            }
            
            return $this->redirect('index');
        }

        // отображаем представление с формой для ввода табличных данных
        return $this->render('batchUpdate', array('items'=>$items));
    }
<?php
use yii\widgets\ActiveForm;

$form = ActiveForm::begin();

foreach($items as $index => $item) {
    echo $form->field($item, "[$index]count");
    echo $form->field($item, "[$index]price");
}

ActiveForm::end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment