0

ファイルアップロードフィールドをいくつか実装していますが、いくつか問題があります。私はこの記事に従っていましたが、モデルを更新しようとするまではすべて完璧でした。ファイル フィールドに入力しなかった場合は、保存後にクリアされました。私はグーグルでこのトピックを見つけました。コードを変更しましたが、別のフィールドを変更しようとすると、画像フィールドも変更しない限り保存されません。問題はどこですか?私のモデルコード:

    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Company']))
        {
            $model->attributes=$_POST['Company'];
            $the_image = CUploadedFile::getInstance($model,'image');
            if (is_object($the_image) && get_class($the_image)==='CUploadedFile')
              $model->image = $the_image;
            if($model->save())
                if (is_object($the_image))
                $model->image->saveAs(Yii::getPathOfAlias('webroot') . '/upload/' . $model->image);
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('update',array(
            'model'=>$model,
    }

そして、これらは私のモデルのルールです:

            array('name', 'required'),
            array('type, number, rating, user_id', 'numerical', 'integerOnly'=>true),
            array('name, image, address, site, mail', 'length', 'max'=>1000),
            array('text', 'safe'),
            array('image', 'file', 'types'=>'jpg, gif, png'),
            array('image', 'unsafe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, type, name, text, address, site, mail, number, rating, user_id', 'safe', 'on'=>'search'),

私を助けてください。なぜ機能しないのかわかりません。

4

2 に答える 2

3

次のように、更新シナリオimageで属性ルールを変更してみてください。

array('image', 'file', 'types'=>'jpg, gif, png','allowEmpty' => true, 'on'=>'update'),

このヒントを見つけましたhere、うまくいくことを願っています。

于 2012-07-16T20:47:16.010 に答える
0
public function uploadMultifile ($model,$attr,$path)
        {
            if($sfile=CUploadedFile::getInstances($model, $attr)){
              foreach ($sfile as $i=>$file){  
                 $formatName=time().$i.'.'.$file->getExtensionName();
                 $file->saveAs(Yii::app()->basePath  .DIRECTORY_SEPARATOR.'..'. $path.$formatName);
                     //  $model->image->saveAs(Yii::app()->basePath . '/../studentimage/' . $date . $model->image);
                 $ffile[$i]=$formatName;
                 }
                return ($ffile);
             }
         }

public function actionCreate()
{
    $model=new Subject;
    // $model->date_erected = date('Y-m-d');
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Subject']))
    {
        $model->attributes=$_POST['Subject'];
                    if($filez=$this->uploadMultifile($model,'imagea','/../images/views/'))
                {
                    $model->documents=implode(",", $filez);
                }
        if($model->save())
            $this->redirect(array('view','id'=>$model->id));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}
于 2014-08-12T13:45:43.450 に答える