yii2-将数据从一个表单保存到多行

时间:2018-02-24 17:47:48

标签: yii2

我是yii2框架的新手,我有一个表单,我想保存每一方得分以对应每个方的缩写。我不知道该怎么做。这是我的代码。

模型 -

namespace app\models;

use Yii;

/**
 * This is the model class for table "announced_pu_results".
 *
 * @property int $result_id
 * @property string $polling_unit_uniqueid
 * @property string $party_abbreviation
 * @property int $party_score
 * @property string $entered_by_user
 * @property string $date_entered
 * @property string $user_ip_address
 */
class AnnouncedPuResults extends \yii\db\ActiveRecord
{


/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'announced_pu_results';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['polling_unit_uniqueid', 'party_abbreviation', 'party_score', 'entered_by_user', 'date_entered', 'user_ip_address'], 'required'],
        [['party_score'], 'integer'],
        [['date_entered'], 'safe'],
        [['polling_unit_uniqueid', 'entered_by_user', 'user_ip_address'], 'string', 'max' => 50],
       // [['party_abbreviation'], 'string', 'max' => 4],
    ];
}

  public function getPollingUnit()
  {

    return $this->hasOne(PollingUnit::className(), ['id' => 'polling_unit_uniqueid']);
  }
/**
 * @inheritdoc
 */


public function attributeLabels()
{
    return [
        'result_id' => 'Result ID',
        'polling_unit_uniqueid' => 'Polling Unit Uniqueid',
        'party_abbreviation' => 'Party Abbreviation',
        'party_score' => 'Party Score',
        'entered_by_user' => 'Entered By User',
        'date_entered' => 'Date Entered',
        'user_ip_address' => 'User Ip Address',
    ];
}

}

控制器动作是创建---

public function actionCreate()
{
    $model = new AnnouncedPuResults();

    if ($model->load(Yii::$app->request->post()) ){
        //&& $model->save()) {
        $post = yii::$app->request->post('AnnouncedPuResults');
        $party_score = $post['party_score'];
        $count = count($party_score);
         for($i =0 ; $i < $count; $i++) {
             $model->party_abbreviation = $post['party_abbreviation'];
             $model->polling_unit_uniqueid = $post['polling_unit_uniqueid'];
             $model->entered_by_user = $post['entered_by_user'];
             $model->date_entered = $post['date_entered'];
             $model->party_score = $post['party_score'];
             $model->save();
         }

        return $this->redirect(['view', 'id' => $model->result_id]);
    }else{

    return $this->render('create', [
        'model' => $model,
    ]);

   }
}

这是动作创建的部分视图形式----

我的表格的屏幕截图已附上 i have a select box against an input box

我想保存选择框中的结果,并将输入的次数保存到数据库中的多行中 这就是数据库的样子 enter image description here 请帮帮我,希望我的问题可以理解。

0 个答案:

没有答案