【问题标题】:CMultiFileUpload class not inserting image names on the database yiiCMultiFileUpload 类未在数据库 yii 上插入图像名称
【发布时间】:2014-04-14 19:47:15
【问题描述】:

我正在开发多文件(图片)上传功能。
我已经尝试了我得到的一切。
它不工作。
它在我指定的路径上上传图片,但没有在表格列上插入图片的名称。
表的列名是“sample”

这是视图的 sn-p:

$this->widget('CMultiFileUpload', array(
    'model' => $model,
    'name' => 'sample',
    'attribute' => 'sample',
    'accept' => 'jpeg|jpg|gif|png',
    'duplicate' => 'Duplicate file!',
    'denied' => 'Invalid file type',
    'remove' => '[x]',
    'max' => 20,
  ));

这是控制器的代码(处理部分的函数):

public function actionCreate($project_id) {
    $model = new Bid();
    $project = $this->loadModel('Project', $project_id);

    if (count($project->bids) == 5) {
        Yii::app()->user->setFlash('warning', Yii::t('bids', 'This project has already reached its maximum number of bids, so you cannot post a new bid.'));
        $this->redirect(array('project/view', 'id' => $project_id));
    }

    if (!empty($project->bid)) {
        Yii::app()->user->setFlash('warning', Yii::t('bids', 'This project has a selected bid already, so you cannot post a new bid.'));
        $this->redirect(array('project/view', 'id' => $project_id));
    }

    if ($project->closed) {
        Yii::app()->user->setFlash('warning', Yii::t('bids', 'You cannot add bids as this project has been closed.'));
        $this->redirect(array('project/view', 'id' => $project_id));
    }

    $model->project = $project;

    if (isset($_POST['Bid'])) {
        $model->attributes = $_POST['Bid'];

        $photos = CUploadedFile::getInstancesByName('sample');
        if (isset($photos) && count($photos) > 0) {

            foreach ($photos as $image => $pic) {
                $pic->name;
                if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/images/'.$pic->name)) {
                    // add it to the main model now
                    $img_add = new Bid();
                    $img_add->filename = $pic->name;
                    $img_add->save();
                }
                else {

                }
            }
        }

        $model->project_id = $project->id;
        $model->freelancer_id = $this->currentUser->id;
        if ($model->save()) {
            $this->redirect(array('project/view', 'id' => $project->id, '#' => 'bidslist'));
        }
    }

    $this->render('create', array(
        'model' => $model,
    ));
}

提前致谢。 如果有人需要其他任何东西以便更好地理解,请告诉我。

【问题讨论】:

    标签: php mysql yii upload cmultifileupload


    【解决方案1】:

    如果我在您保存模型时正确理解了您

     $img_add = new Bid();
     $img_add->sample = $pic->name;
     $img_add->save();
    

    【讨论】:

    • sample 是我要在其中插入数据的数据库字段的名称。
    • 我不确定我是否得到了你的要求。我只是想知道数据过去是如何保存到数据库中的,以及为什么现在不保存。非常感谢。
    • 你说文件名没有保存?因此,如果文件名字段是示例写入 $img_add->sample = $pic->name;
    • Alex,图像文件的名称未保存在名为“sample”的数据库字段中。我想要的是要存储在数据库“样本”列中的图像名称。
    猜你喜欢
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    相关资源
    最近更新 更多