缺少id在urlindent传递

时间:2016-12-29 06:51:47

标签: php jquery yii2

我正在尝试在服务器上传文件并查看上传的文件我必须将模型ID传递给视图操作。这一切都很好,直到我添加

'options' => ['enctype'=>'multipart/form-data']

到我的ActiveForm。添加后,通过URL传递给操作视图的id不再显示,我得到了缺少的参数消息。 我正在使用yii 2.0.2

创建行动:

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

    if ($model->load(Yii::$app->request->post()) ) {
        //$fileName = $model->id;
        $model->save();
        $fileName = $model->id;

        $files =UploadedFile::getInstance($model,'files');

        $files->saveAs( 'uploads/'.$files->getBaseName().'.'.$files->getExtension());
        $model->files=$files;

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

形式:

<div class="sub-content-form">

<?php $form = ActiveForm::begin(['options' => ['enctype'=>'multipart/form-data']]); ?>

    <?= $form->field($model, 'files')->fileInput(['maxlength' => 255]) ?>
    <?= $form->field($model, 'sub_id')
              ->dropDownList(ArrayHelper::map(Subjects::find()
              ->where(['year'=>\Yii::$app->user->identity->year])
              ->all(),'id','sub_name'),
              ['prompt'=>' select subject']
          ) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
<?php ActiveForm::end(); ?>

</div>

这是我的模特

         <?php

       namespace frontend\models;

       use Yii;

            /**
        * This is the model class for table "sub_content".
           *
         * @property integer $id
        * @property string $files
           * @property integer $sub_id
           *
          * @property Subjects $sub
           */
            class SubContent extends \yii\db\ActiveRecord
           {
           /**
            * @inheritdoc
             */
             public static function tableName()
             {
            return 'sub_content';
               }

             public function rules()
            {
               return [
              [['files', 'sub_id'], 'required'],
             [['sub_id'], 'integer'],
           [['files'], 'string', 'max' => 255]


         ];
        }

        public function attributeLabels()
          {
           return [
           'id' => 'ID',
           'files' => 'Files',
            'sub_id' => 'Sub ID',
            ];
             }

           /**
          * @return \yii\db\ActiveQuery
           */
            public function getSub()
              {
               return $this->hasOne(Subjects::className(), ['id' => 
           'sub_id']);
           }
            }

编辑.....仍然没有工作

1 个答案:

答案 0 :(得分:0)

首先 - 自2.0.8起,您不必添加enctype,当您添加fileInput字段时,它会自动完成。

验证$model->save()是否返回true并且看起来该模型未通过验证,因为{{1} 未保存,因为$model 未设置

您应该执行以下操作:

$model->id

为此,您需要在public function actionCreate() { $model = new SubContent(); if ($model->load(Yii::$app->request->post()) ) { $model->files = UploadedFile::getInstance($model,'files'); if ($model->save()) { $model->files->saveAs('uploads/' . $files->getBaseName() . '.' . $files->getExtension()); return $this->redirect(['view', 'id' => $model->id]); } } return $this->render('create', ['model' => $model]); } 模型中设置适当的验证规则。