1

我在 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)

错误图片

模型类:

...
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

4

1 回答 1

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

我希望它越来越好

于 2013-10-08T18:16:34.290 回答