1

I'm working on multi file(images) uploading functionality.
I've tried everything I got.
It's not working.
It's uploading images on the path I specified but not inserting image's names on the table column.
The table's column name is "sample"

Here's the snippet of the view:

$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,
  ));

This is the controller's code the(the function that dealing with the part):

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,
    ));
}

Thanks in Advance. Please let me know if anyone needs anything else for better understanding.

4

1 回答 1

1

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

 $img_add = new Bid();
 $img_add->sample = $pic->name;
 $img_add->save();
于 2014-04-14T20:57:11.940 回答