yii2更新表格数据集不起作用

时间:2016-01-17 05:44:24

标签: yii2 yii2-advanced-app

http://www.yiiframework.com/doc-2.0/guide-input-tabular-input.html#collecting-tabular-input

我根据上面的链接工作,但我的数据没有更新到数据库

我的表结构是

设置

ID,名称,值

我的控制器SettingsController.php

 <?php

 namespace backend\controllers;

 use Yii;
 use yii\base\Model;
 use yii\web\Controller;
 use backend\models\Setting;

class SettingsController extends Controller
{


public function actionIndex()
{
    $settings = Setting::find()->indexBy('id')->all();

    if (Model::loadMultiple($settings, Yii::$app->request->post()) && Model::validateMultiple($settings)) {
        foreach ($settings as $setting) {
            $setting->save(false);
        }
        return $this->redirect('update');
    }

    return $this->render('update', ['settings' => $settings]);
  }
}

我的模特是Setting.php

<?php
namespace backend\models;
use Yii;
class Setting extends \yii\db\ActiveRecord
{

  public static function tableName()
  {
     return 'setting';
  }

}

视图在设置下 - update.php

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin();
 foreach ($settings as $index => $setting) {
      echo $form->field($setting, "[$index]value")->label($setting->name);
 }
 ?>
<div class="form-group">
    <?= Html::submitButton('Update', ['class' => 'btn btn-primary']) ?>
</div>
<?php
ActiveForm::end();

我的问题是更新数据没有保存。但我没有收到任何错误。

1 个答案:

答案 0 :(得分:1)

您的Setting模型中没有任何规则或安全属性,因此该模型的值不可用..

尝试在模型的规则中添加save属性列表..

public function rules()
{
   return [
       [['field1', 'field2',,,,'fieldn'], 'safe'],
   ];
}