if i upload image then edit work fine but if i update other field and not image that time it display array to sting erro in cakephp2.4.5

别说谁变了你拦得住时间么 提交于 2019-12-11 23:14:47

问题


it will display error if i update other filed and not image.

public function edit($id = null) {

$this->helpers = array('TinyMCE.TinyMCE');
$this->layout = 'adminpanel';
if (!$id) {
    throw new NotFoundException(__('Invalid post'));
}

$this->layout = 'adminpanel';
//save data
if ($this->request->is(array('post', 'put'))) {

    $this->Tour->id = $id;

    //Save image
    if(is_uploaded_file($this->request->data['Tour']['varbigimg']['tmp_name']))
    {

            $fileNameFull = $this->request->data['Tour']['varbigimg']['name'];

                 $uploadFolder = "upload";
                 //full path to upload folder
                $uploadPath = WWW_ROOT . $uploadFolder;
                $oldFile = $uploadPath.'/'.$fileNameFull; 

            move_uploaded_file(
              $this->request->data['Tour']['varbigimg']['tmp_name'],$oldFile
            );

            $newFile = WWW_ROOT.'courseImages/thumb/'.$fileNameFull; 

            $image = new ImageResizeComponent();
            $quality = 100; // image resize for thumb
            $height = 40;
            $width = 60;
            $this->ImageResize->resize($oldFile, $newFile, 60,60,$quality);
            $this->request->data['Tour']['varbigimg'] = $fileNameFull;
        } 
    else{//Img not uploaded
 $this->request->data['Tour']['vartitle']=  $this->data['Tour']['vartitle'];   
 $this->request->data['Tour']['varsubtitle']=  $this->data['Tour']['varsubtitle']; 
 $this->request->data['Tour']['txtsortdesc']=  $this->data['Tour']['txtsortdesc']; 
$this->request->data['Tour']['txtdeasc']=  $this->data['Tour']['txtdeasc'];   
 $this->request->data['Tour']['vardeparts']=  $this->data['Tour']['vardeparts']; 
 $this->request->data['Tour']['decadultprice']=  $this->data['Tour']['decadultprice']; 
 $this->request->data['Tour']['decchildprice']=  $this->data['Tour']['decchildprice'];   
 $this->request->data['Tour']['varimgtitle']=  $this->data['Tour']['varimgtitle']; 
 $this->request->data['Tour']['enumstatus']=  $this->data['Tour']['enumstatus']; 
   $this->request->data['Tour']['id']=  $this->data['Tour']['id']; 
//In this way do for All Except Image.
}
  //     pr($this->$this->request->data);
    if ($this->Tour->save($this->request->data)) {              
    $this->Session->setFlash(__('Unable to add your schedule.'));

    //Save image
    $this->Session->setFlash(__('Your tour has been updated.'));
    return $this->redirect(array('controller'=>'admin','action' => 'tour'));    
    $this->Session->setFlash(__('Unable to update your Tour.'));
    }
}
$tour = $this->Tour->findByid($id);
if (!$tour) {
    throw new NotFoundException(__('Invalid post'));
}
if (!$this->request->data) {
    $this->request->data = $tour;
}

}

my cont code my view is below. so when i upload image it will work fine. but in edit if i dont upload image then it display array to sting error. means it not take ast image. thanks

echo $this->Form->create('Tour',array('autocomplete' => 'off','enctype'=>'multipart/form-data'));
echo $this->Form->input('varbigimg',array('type' => 'file'));?>

回答1:


Write Else for

if(is_uploaded_file($this->request->data['Tour']['varbigimg']['tmp_name']))
{
   .........
}else{

     //**TWO GOLDEN LINES OF YOUR LIFE**
     $tourForImg = $this->Tour->findByid($id);
     $this->request->data['Tour']['varbigimg'] = $tourForImg['Tour']['varbigimg'];
     //**TWO GOLDEN LINES OF YOUR LIFE**


     //AS Img not uploaded by user
     //Write All data EXPLICITELY that you want to save WITHOUT Image.
     $this->request->data['Tour']['Tourname']=  $this->data['Tour']['Tourname'];   
     $this->request->data['Tour']['YourFormField1']=  $this->data['Tour']['YourFormField1']; 
     $this->request->data['Tour']['YourFormField2']=  $this->data['Tour']['YourFormField2']; 
     //In this way do for All Except Image.



}


来源:https://stackoverflow.com/questions/26649845/if-i-upload-image-then-edit-work-fine-but-if-i-update-other-field-and-not-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!