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

时间:2014-11-10 11:14:31

标签: php yii

我对Yii很新,只是添加了一个管理员可以上传多个文件的功能。但是,该模型正在保存上载到相应文件夹的图像,但不会将图像的路径/名称保存到数据库中。

我该怎么做?

数据库看起来像:

id | naam | beschrijving | prijs | actieprijs |类别|子类别|图片| toegevoegd | aangepast | 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,
        ));

请帮助!!

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

我已经弄明白了,它一直在覆盖数据库中的图像,所以如果我上传了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,
    ));
}