2017-04-05 1 views
-2

Ich habe versucht, mit einer Website umzugehen, die von einem anderen Programmierer geschrieben wurde. Die Website ist mit yii 1.1.4yii1.1: kann einen Datensatz nicht aktualisieren, ohne eine Umlage zu laden

gebaut Es scheint, dass ich einen "Speicher" Datensatz nicht aktualisieren kann, ohne jedes Mal ein neues Bild hochzuladen. Hier ist die Meldung, die ich erhalte, wenn ich versuche, ein Update durchzuführen, ohne ein Bild hochzuladen: "Bitte korrigieren Sie die folgenden Eingabefehler:

Bild kann nicht leer sein."

das Bildfeld (aka "icon_filename") ist nicht einmal erforderlich. auch, die meisten Datensätze haben noch kein Bild hochgeladen.

jemand weiß, was ist das Problem?

hier ist der Code:

Modelle:

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

Controller:

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

ich weiß nicht, wie. Ich habe keine Ahnung, was mit dem Code falsch ist. nach den Regeln wird das Bild nicht benötigt. – codingnighter2000

Antwort

0

sein Sie können verschiedene Attribute getInstance und Speicher in DB verwenden müssen?

E.G.

public $photo; 

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

in Controller

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

dann Name, Pfad speichern, oder was Ihr wünschen

$model->pictureName = $model->photo->name; 
Verwandte Themen