2013-02-03 104 views
0

控制器將數據傳遞到模型笨

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

    class Upload extends CI_Controller { 

      function __construct(){ 
       parent::__construct(); 
       $this->load->helper(array('form', 'url')); 
      } 

      function index() 
      { 
       $this->load->view('uploaderview', array('error' => ' ')); 
      } 

      function do_upload(){ 
       $config['upload_path'] = './upl0d/'; 
       $config['allowed_types'] = 'gif|jpg|png'; 
       $config['max_size'] = '2048'; //2mb 
       $config['max_width'] = '1024'; 
       $config['max_height'] = '768'; 
       $config['encrypt_name'] = FALSE; 
       $config['overwrite'] = FALSE; 

       $this->load->library('upload', $config); 

       if (! $this->upload->do_upload()){ 
        $error = array('error' => $this->upload->display_errors()); 
        $this->load->view('uploaderview', $error); 
       } 
       else{ 
        ## Insert into filesystem. 
        $data = array('upload_data' => $this->upload->data()); 
        ## load the success page. 
        $this->load->view('uploadsuccess', $data); 
        ## Insert into db 
        ## then insert the img name into the database 
        $this->load->model('uploadermodel'); 
        $this->uploadermodel->uploadcoupon();    
       } 
      } 
    } 
    ?> 

模式

<?php 

    class Uploadermodel extends CI_Model{ 

     function __construct(){ 
      // Call the Model constructor 
      parent::__construct(); 
     } 

     function uploadcoupon($data){ 
      $uploadFileName = $this->upload->data(); 
      $currentDt = date('Y-m-d H:i:s'); 
      $data = array('fileNameUploaded'=>$uploadFileName,'date'=>$currentDt); 
      $this->db->insert('Coupon', $data); 
     } 

    } 
    ?> 

我試圖收集的$config['file_name']的價值,並與我的模型發送。我該怎麼做?目前,它正試圖上傳:VALUES (Array, '2013-02-03 20:20:01')

回答

1

您現在有一個關鍵的「upload_data」(你實際上並不需要這樣做 - 我希望他們能解決這個文檔,但離開它)數組$數據

所以,做一個var_dump($ data ['upload_data']),你會看到關於你剛剛上傳的文件的所有信息。

從內存中去,就會出現類似$數據[「upload_data」] [「FILE_NAME」] ...

所以,只要通過該值加上完整的插入數據模型。

$this->uploadermodel->uploadcoupon(); 

需要有$數據傳遞...

$this->uploadermodel->uploadcoupon($data); 

不要試圖從$這個 - 抓住它>上傳喜歡自己正在做

0

爲此,你需要這個

$image_data = $this->upload->data(); 
$data['image_name'] = $image_data['file_name']; 

//Now you can insert file name 
$this->uploadermodel->uploadcoupon($data); 
0

從文檔:

$ this-> upload-> data()是一個幫助函數,它返回一個數組,其中包含與您上傳的文件相關的所有數據。這裏是 的數組原型。

因此,您需要指定要存儲在數據庫中的數組索引。更多信息,請訪問:

http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

+0

你是正確的,但我認爲這是壞的設計,上載製成模型函數內部承擔。 模型函數應該只處理插入,並傳遞驗證的數據來插入。我應該在我的回答中更清楚地說明 – jmadsen

+0

是的你是對的......我的回答是關注他在插入數據時得到的錯誤..我不專注於設計模式和其他事情。 –