yii1.1:无法上传umage而无法更新记录

时间:2017-04-05 11:02:13

标签: php yii yii1.x

我一直试图处理由另一位程序员编写的网站。该网站使用yii 1.1.4构建

似乎每次都不上传新图片我无法更新“商店”记录。这是我在没有上传图片的情况下尝试更新时收到的消息: “请修正以下输入错误:

图片不能为空。“

甚至不需要图片字段(也称为“icon_filename”)。 此外,大多数记录还没有上传任何图片。

任何人都知道这是什么问题?

这是代码:

模型:

    public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('name, stream_id, full_address,about, country_id', 'required', 'on' => array('create')),
        array('status, stream_id, lock_version', 'numerical', 'integerOnly' => true),
        array('name, email', 'length', 'max' => 255),
        array('about, full_address, website_url', 'length', 'max' => 500),
        array('website_url, maps_service_url', 'url'),
        array('phone, fax', 'length', 'max' => 30),
        array('phone, fax', 'PcSimplePhoneValidator'),
        array('country_id, region_id, assigned_user, created_by, last_updated_by_user_id', 'length', 'max' => 10),
        array('assigned_user', 'validateAssignedUser', 'except' => 'search'),
        array('city_id', 'length', 'max' => 6),
        array('icon_filename', 'file',
            'types' => "jpg,jpeg",
            'wrongType' => Yii::t('StoresModule.forms', "Invalid file type. These are the supported file types: {extensions}"),
            'maxSize' => 1048576,
        ),
        array('icon_filename', 'file', 'allowEmpty' =>true, 'on' => 'update'),
        array('updated_on', 'safe'),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, name, email, stream_id, searchText, status, about, full_address, phone, fax, website_url, country_id, region_id, city_id, icon_filename, assigned_user', 'safe', 'on' => 'search'),
    );
}

控制器:

enter/**
 * Updates a particular model.
 * If update is successful, the browser will be redirected to the 'view' page.
 *
 * @param integer $id the ID of the model to be updated
 * @throws CHttpException
 */
public function actionUpdate($id) {
    /* @var Store $model */
    $model = $this->loadModel($id);
    // set scenario to update to allow empty image - meaning no replacement of Store image
    $model->scenario = 'update';

    if ($model === null) {
        Yii::log("Store update requested with id $id but no such record found!", CLogger::LEVEL_INFO, __METHOD__);
        throw new CHttpException(404, Yii::t("StoresModule.general", 'The requested page does not exist.'));
    }

    if (Yii::app()->user->checkAccess('edit Store')) {
        // do nothing. edit is allowed.
    }
    else if (!is_null($model->assignedUser)) {
        // some user is assigned to this Store
        if (Yii::app()->user->checkAccess('edit assigned Store', array('assigned_user' => $model->assignedUser->id, 'user_id' => Yii::app()->user->id))) {
            // do nothing. this user is the assigned user and therefore is allowed to edit.
        }
    }
    else {
        Yii::log("User (id=" . Yii::app()->user->id . ") tried to edit Store with id=$id but no such record. Giving him a **404** error", CLogger::LEVEL_WARNING, "SECURITY " . __METHOD__);
        throw new CHttpException(404, Yii::t("StoresModule.general", 'The requested page does not exist.'));
    }

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

        // use aux variable for manipulating the image file.
        $image = CUploadedFile::getInstance($model, 'icon_filename');
        // check if a new image weas submitted or not:
        if ($image) {
            /* the only thing that might have changed in the update is the extension name of the image. therefore,
                                 * if something was submitted, and since we already know the ID of the Store, we can determine the full
                                 * updated icon_filename attribute of the model prior to its save() (unlike in create action - see there...).
                                 */
            $model->icon_filename = $model->getImageFsFilename($image);
        }

        if ($model->save()) {
            // save the updated image, if any
            if ($image) {
                $image->saveAs($model->getImageFsFilename($image));
                // create the thumbnail image file:
                /* @var simple_image $thumbnail */
                $thumbnail = Yii::app()->imageResizer->load($model->icon_filename);
                $thumbnail->resizeToWidth(Store::THUMBNAIL_WIDTH_LIMIT);
                $thumbnail->save($model->getImageFsThumbFilename($image));
            }
            $this->redirect(array('view', 'slug' => $model->generateUniqueSlug()));
        }
    }

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

1 个答案:

答案 0 :(得分:0)

您可能必须使用不同的属性来getInstance并存储在DB中吗?

E.G。

Button btn = (Button) view.findViewById(R.id.button);
btn.setClickable(false);
控制器中的

    public $photo;

    array('photo', 'file', 'maxSize' => 1048576, 'types' => 'jpg, png, gif, jpeg', 'allowEmpty' => true)

然后保存名称,路径或您想要的内容

    $model->photo = CUploadedFile::getInstance($model, 'photo');