yii数据库中的多个上传保存路径不起作用


yii multiple upload save paths in database does not work

我对Yii很陌生,刚刚添加了一个管理员可以上传多个文件的功能。然而,该模型将上传到适当文件夹的图像保存,但不将图像的路径/名称保存到数据库中。

我该怎么做?

数据库看起来像:

id | naam | beschrijving | prijs | activiprijs | categorie | subcategore | images | toegevoegd | aangepass | aangepast_door

控制器:

$model=$this->loadModel($id);
        $dir = Yii::getPathOfAlias('webroot.images.producten');
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        if(isset($_POST['Producten']))
        {
            $model->attributes=$_POST['Producten'];
            $images = CUploadedFile::getInstancesByName('images');
            // proceed if the images have been set
            if (isset($images) && count($images) > 0) {
                // go through each uploaded image
                foreach ($images as $image => $pic) {
                    $fn = str_replace(" ", "-", $pic->name);
                    if ($pic->saveAs($dir.'/'.$fn)) {
                        // add it to the main model now
                        $img_add = new Producten();
                        $img_add->images = $fn; //it might be $img_add->name for you, filename is just what I chose to call it in my model
                        $img_add->save(); // DONE
                    }
                }
            }
            else{
                // handle the errors here, if you want
            }
                // save the rest of your information from the form
            if ($model->save()) {
                $this->redirect(array('view','id'=>$model->id));
            }
            /*
            $model->images=CUploadedFile::getInstance($model,'images');
            $nf = str_replace(" ", "+", $model->images);
            if($model->save()){
                $model->images->saveAs($dir.'/'.$nf);
                $model->images = $nf;
                $this->redirect(array('view','id'=>$model->id));
            }
            */
        }
        $this->render('create',array(
            'model'=>$model,
        ));

请帮忙!!

看来你的代码还可以。这里我建议你添加错误报告,看看是否存在任何错误。只需尝试以下代码

$img_add->images = $fn;                        
if(!$img_add->save()){
   var_dump($img_add->getErrors());
}

不需要在foreach()中多次创建Producten模型的对象。可以将$img_add = new Producten();放置在foreach()之前。也可以在分配之前使用unset($img_add->images)$img_add->images = $fn;(如果需要)。

我已经想好了,它一直覆盖数据库中的图像,所以如果我上传了5张照片,那么数据库中只有最后一张照片。然而,它确实将它们全部5个保存到了正确的路径上。现在我做了这个,它似乎起作用了:

public function actionCreate()
    {
        $model=new Producten;
        $dir = Yii::getPathOfAlias('webroot.images.producten');
        if(isset($_POST['Producten']))
        {
            $old_images = explode(',',strtoupper($model->images));
            $model->attributes=$_POST['Producten'];
            $images = CUploadedFile::getInstancesByName('images');
            if (isset($images) && count($images) > 0) {
                $img_add = new Producten();
                foreach ($images as $image => $pic) {
                    $fn = str_replace(" ", "-", strtoupper($pic->name));
                    if ($pic->saveAs($dir.'/'.$fn)) {
                    if(!in_array($fn, $old_images)){
                        $model->images .= $fn.",";
                    }
                }
            }
        }
        $model->toegevoegd=date('Y-m-d H:i:s', strtotime('now'));
        $model->aangepast=date('Y-m-d H:i:s', strtotime('now'));
        $model->aangepast_door=Yii::app()->user->id;
        if ($model->save()) {
            $this->redirect(array('view','id'=>$model->id));
        }
    }
    $this->render('create',array(
        'model'=>$model,
    ));
}