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

Php yii数据库中的多个上载保存路径不起作用,php,yii,Php,Yii,我是Yii的新手,刚刚添加了一个管理员可以上传多个文件的功能。但是,模型将保存上载到相应文件夹的图像,但不会将图像的路径/名称保存到数据库中 我该怎么做 数据库看起来像: id | naam | beschrijving | prijs | actieprijs | categories | subcategories | images | toegevoegd | angepass | angepass | 控制器: $model=$this->loadModel($id);

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

我该怎么做

数据库看起来像:

id | naam | beschrijving | prijs | actieprijs | categories | subcategories | images | toegevoegd | angepass | angepass |

控制器:

$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\u add->images)

$img\u add->images=$fn如果需要。

我想出来了,它一直在覆盖数据库中的图像,因此如果我上传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,
    ));
}

是否将新记录插入到相应的表中?如果是,图像字段的值是多少?另外,您能给我们看一下Producten模型的方案吗?不,它根本不会将其保存到数据库中,方案是有问题的。在这种情况下,它不只是更新同一条记录并用最后一条覆盖图像字段吗?