2014-03-31 86 views
0

我想使用datamapper來幫助我並將file_name存儲在數據庫中。首先,我使用Codeigniter手冊中給出的do_upload的基本示例做了最後,我寫了3行datamapper來查看帶有id和url的簡單表。我運行它,它可以很好地保存它。Codeigniter和Datamapper:將上傳的圖像保存到數據庫不起作用

現在的問題是,當我嘗試在我的add_article方法中調用do_upload函數時,因此當用圖像上載文章以保存文章+上傳圖像名稱的所有細節時。然而,它不起作用,我嘗試所有類型。以下是我有:

在我的控制器:

public function add_article($id = NULL) 
{ 
    $articles = new Article_model(); 
    $article = $articles->where('id', $id)->get(); 

    $article->title = $this->input->post('title'); 
    $article->text = $this->input->post('text'); 


    // Try to upload the image 
    if ($this->input->post('submit')) 
    { 
     $this->do_upload(); 
    } 

    if ($article->save()) 
    { 
     echo '<p>You have successfully added an article</p>'; 
     redirect('admin/article/'); 
    } 
    else 
    { 
     echo '<p>' . $article->error->string . '</p>'; 
    } 
} 

    public function do_upload() 
{ 
    $config = array(
     'allowed_types' => 'jpg|jpeg|gif|png', 
     'upload_path' => $this->gallery_path, 
     ); 

    $this->load->library('upload' , $config); 
    $this->upload->do_upload(); 
    $image_data = $this->upload->data(); 

    $article = new Article_model(); 
    $article->url = $image_data['file_name']; 
    $article->save(); 


} 

我的觀點:

<?php echo form_open_multipart('admin/article/add_article'); ?> 
<table class="table"> 
    <tr> 
     <td>Title</td> 
     <td><?php echo form_input('title', set_value('title', $article->title)); ?></td> 
    </tr> 
    <tr> 
     <td>Image</td> 
     <td><?php echo form_upload('userfile'); ?></td> 
    </tr> 
    <tr> 
     <td>Body</td> 
     <td><?php echo form_textarea('text', set_value('text' , $article->text), 'class="tinymce"'); ?></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td> 
    </tr>  
</table> 
+0

你得到的錯誤是什麼? – mituw16

+0

我沒有收到錯誤,file_name不在數據庫中:( – Unsparing

+0

我看,我懷疑這是因爲在你的'do_upload'函數中,你正在實例化一個新的'Article_Model',而不是更新一個你之前插入的,嘗試改變你的代碼,我會在我的答案。 – mituw16

回答

0

試試這個。

而是在do_upload方法實例化一個新Article_Model的,只是從do_upload方法返回圖像的名稱,並將其保存到數據庫之前設置Article_Model的url屬性。

public function add_article($id = NULL) 
{ 
    $articles = new Article_model(); 
    $article = $articles->where('id', $id)->get(); 

    $article->title = $this->input->post('title'); 
    $article->text = $this->input->post('text'); 


    // Try to upload the image 
    if ($this->input->post('submit')) 
    { 
     $article->url = $this->do_upload(); 
    } 

    if ($article->save()) 
    { 
     echo '<p>You have successfully added an article</p>'; 
     redirect('admin/article/'); 
    } 
    else 
    { 
     echo '<p>' . $article->error->string . '</p>'; 
    } 
} 

public function do_upload() 
{ 
    $config = array(
    'allowed_types' => 'jpg|jpeg|gif|png', 
    'upload_path' => $this->gallery_path, 
    ); 

    $this->load->library('upload' , $config); 
    $this->upload->do_upload(); 
    $image_data = $this->upload->data(); 

    return $image_data['file_name']; 
} 
+0

它工作乾杯:) – Unsparing

相關問題