2016-07-30 2 views
1

私はyii2のデータベーステーブルからGiiを使ってモデルを生成しました。このモデルはActiveRecordを継承しています。このモデルからフォームを作成しました。このフォームでファイルをアップロードしたいのですが。 同じモデル(ActiveRecordを継承したファイル)をアップロードすることは可能ですか?yii2でActiveRecordを継承するモデルでファイルをアップロードすることは可能ですか?

はいの場合、どうですか? いいえ、どうすればよいですか?

+0

あなたの質問を更新して、関連するモデル、コントローラ/アクション、ビューを表示してください – scaisEdge

答えて

0

はい、可能ですが、別のフォームを使用する方が優れています。よくUploading files

は別のフォームを作成します記事の公式ドキュメントで説明しています:

namespace app\controllers; 

use Yii; 
use yii\web\Controller; 
use app\models\UploadForm; 
use yii\web\UploadedFile; 

class SiteController extends Controller 
{ 
    public function actionUpload() 
    { 
     $model = new UploadForm(); 

     if (Yii::$app->request->isPost) { 
      $model->imageFile = UploadedFile::getInstance($model, 'imageFile'); 
      if ($model->upload()) { 
       // file is uploaded successfully 
       return; 
      } 
     } 

     return $this->render('upload', ['model' => $model]); 
    } 
} 
:ファイルのアップロードを処理するために

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

    <?= $form->field($model, 'imageFile')->fileInput() ?> 

    <button>Submit</button> 

<?php ActiveForm::end() ?> 

カスタマイズコントローラ:ファイル入力をレンダリング

namespace app\models; 

use yii\base\Model; 
use yii\web\UploadedFile; 

class UploadForm extends Model 
{ 
    /** 
    * @var UploadedFile 
    */ 
    public $imageFile; 

    public function rules() 
    { 
     return [ 
      [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'], 
     ]; 
    } 

    public function upload() 
    { 
     if ($this->validate()) { 
      $this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension); 
      return true; 
     } else { 
      return false; 
     } 
    } 
} 

複数のファイルのアップロードについてもこの記事で取り上げています。

this related questionも参照してください。

ActiveRecordと一緒に使用し、ファイルパスをモデル属性として保存する場合は、upload()フォームの方法でモデルの作成と保存を追加できます。フォームでの作業のもう一つの良い例は、高度なテンプレートからSignupFormです、これはより良い練習がモデルで直接処理するのではなく、ある

<?php 

namespace frontend\models; 
use yii\base\Model; 
use common\models\User; 

/** 
* Signup form 
*/ 
class SignupForm extends Model 
{ 
    public $username; 
    public $email; 
    public $password; 


    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      ['username', 'trim'], 
      ['username', 'required'], 
      ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'], 
      ['username', 'string', 'min' => 2, 'max' => 255], 
      ['email', 'trim'], 
      ['email', 'required'], 
      ['email', 'email'], 
      ['email', 'string', 'max' => 255], 
      ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'], 
      ['password', 'required'], 
      ['password', 'string', 'min' => 6], 
     ]; 
    } 

    /** 
    * Signs user up. 
    * 
    * @return User|null the saved model or null if saving fails 
    */ 
    public function signup() 
    { 
     if (!$this->validate()) { 
      return null; 
     } 

     $user = new User(); 
     $user->username = $this->username; 
     $user->email = $this->email; 
     $user->setPassword($this->password); 
     $user->generateAuthKey(); 

     return $user->save() ? $user : null; 
    } 
} 

あなたが見ることができるように、検証およびモデルの作成は、別の形に隔離されている(特にファイルアップロード)。しかし、単にモデルを使って簡単な目的のためにも許容することができます。

+1

lot.myコードがエラーを生成してくれてありがとう、おそらくActiveRecordから継承したモデルに関連していると思います。 –

関連する問題