在Yii框架中使用扩展名mongodbsuite时,CUploadedFile分配图像属性时出错

时间:2013-09-27 19:31:43

标签: php mongodb yii-extensions yii

我在yii框架中使用mongodb套件,当将CUploadedFile分配给公共属性图像类模型时,得到错误:

MongoException

zero-length keys are not allowed, did you use $ with double quotes?

/var/www/html/bablog/protected/extensions/YiiMongoDbSuite/EMongoDocument.php(611)

Error Pic

模特课:

...
public function rules() {

        return array(
           ...
            array('image', 'file',
              'types'=>'jpg,jpeg, png' ,
              'mimeTypes' => 'image/jpeg , image/pjpeg,image/png' ,
              'safe'=>true,
              'maxFiles' => 1 ,
              'maxSize' => 1024 ,
              'minSize' => 100 ,
              ),
        );
}

添加代码:

echo '<pre>';
print_r(CUploadedFile::getInstance($model,'image'));
die();

输出:

CUploadedFile Object
(
    [_name:CUploadedFile:private] => download.jpg
    [_tempName:CUploadedFile:private] => /tmp/phpC8GRRt
    [_type:CUploadedFile:private] => image/jpeg
    [_size:CUploadedFile:private] => 530
    [_error:CUploadedFile:private] => 0
    [_e:CComponent:private] => 
    [_m:CComponent:private] => 
)

尝试:

1-在模型中声明图像动态属性,但在父级中定义__set函数再次得到相同的错误。

2-定义受保护的属性但是CUploadedFile :: getInstance($ model,'image')要求公共属性,结果得到错误:(:D。

问题:

1 - 对于上述问题,我该怎么办?

2-有没有办法在mongodbsuite中定义自己的属性,而不是在类中获取属性公共属性?

抱歉我的英语不好

tnx for All

1 个答案:

答案 0 :(得分:0)

最后我是多个上传文件:

查看文件:

.....
    <div class="row">
            <?php echo $form->labelEx($model, 'image[0]'); ?>
            <?php echo $form->fileField($model, 'image[0]'); ?>
            <?php echo $form->error($model, 'image[0]'); ?>
        </div>

                        <div class="row">
            <?php echo $form->labelEx($model, 'image[1]'); ?>
            <?php echo $form->fileField($model, 'image[1]'); ?>
            <?php echo $form->error($model, 'image[1]'); ?>
        </div>
.....

控制器文件:

<?php

    public function actionCreate() {
        $model = new Post;
        $images = array();
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);

        if (isset($_POST['Post'])) {
            $countImage = count($_POST['Post']['image']);
            $model->attributes = $_POST['Post'];
            $model->status = $_POST['Status'];

            $j = 0;
            for ($i = 0; $i < $countImage; $i++) {

                $imageTemp = new Post;
                $imageTemp->image = CUploadedFile::getInstance($imageTemp, "image[$i]");

                if (!empty($imageTemp->image)) {
                    $images[$j] = $imageTemp;
                    $j++;
                }
                unset($imageTemp);
            }

            if ($model->save()) {

                foreach ($images as $i => $image) {

                    $namePic[$i] = Yii::app()->params['pathImages'] . Yii::app()->params['domainName'] . '-' . (string) $model->_id . '-' . time() . '-' . rand(1, 1000) . '.' . $image->image->getExtensionName();
                    $image->image->saveAs($namePic[$i]);
                }

                $model->image = $namePic;
                $model->update(array('image'), true);
                unset($images);

//$this->redirect(array('view', 'id' => $model->_id));
            }
        }

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

模特:

public function rules() {

        return array(....
            array('image', 'file',
              'types'=>'jpg, png' ,
              'mimeTypes' => 'image/gif, image/jpeg' ,
              'safe'=>true,
              'maxFiles' => 3 ,
              'maxSize' => 10240000 ,
              ),
....
        );
    }

结束:D

我希望它变得越来越好