2013-02-27 49 views
4

我有一個表單,用戶在其中選擇一個文檔。然後,該文檔由控制器保存到webroot文件夾中。在CakePHP中創建一個與模型無關的表單

然而,它甚至可以顯示錶單之前,它給了我一個錯誤:

Missing Database Table 
Error: Table office_layouts for model OfficeLayout was not found in datasource default. 

哪個是正確的,對於office_layouts沒有數據庫表。但它不是必需的。這就是爲什麼沒有桌子。表單只是上傳到服務器。

我已經通過創建表單讀取,並且已經嘗試了以下內容:

// file: View/OfficeLayout/upload.ctp 
echo $this->Form->create(null ,array('type' => 'file')); ?> 
<fieldset> 
    <legend><?php echo __('&emsp;Add Layout for ' . $branch); ?></legend> 
    <?php 
     echo $this->Form->input('layout',array('type'=>'file')); 
    ?> 
</fieldset> 
<?php echo $this->Form->end(__('Submit')); ?> 

除了chaning的null到控制器的名稱(在此情況下OfficeLayout)。我也已刪除所有參數(thus creating it like so: $this->Form->create()

在我的控制器我有以下幾點:

public function upload($branch = null) { 
    $this->set('branch',$branch); 
    if(isset($this->request->data['OfficeLayout'])) { 
     $file = $this->request->data['OfficeLayout']['layout']; 
     if($file['error'] === UPLOAD_ERR_OK && move_uploaded_file($file['tmp_name'],APP . 'docs' . DS . 'layouts' . DS . $branch . '.pdf')) { 
      $this->Session->setFlash('New layout successfully uploaded.','default',array('class'=>'notification')); 
      $this->redirect(array('action'=>$branch)); 
     } else { 
      $this->Session->setFlash('Error uploading layout. Please contact web admin.','default',array('class'=>'error')); 
     } 
    } 
} 

這個動作獲取的調用:domain/office_layouts/upload/branch

每當我刪除$this->Form->create()行時,它顯示上傳視圖,但顯然不提交。

所以我在這種情況下的問題是,我怎麼能創建一個窗體,沒有它在數據庫中查找表?

回答

12

相反null的,使用false

echo $this->Form->create(false, array('type' => 'file')); ?> 
+0

啊,太棒了! :d – Albert 2013-03-01 12:56:40

0

通過創建一個模型文件,只是把這個在那裏固定它:

class OfficeLayout extends AppModel { 
    var $useTable = false; 
} 
+0

沒有必要創建一個空的模型。創建表單時只需使用false。更簡單:)我添加了一個答案。 – Alvaro 2013-03-01 12:37:18

相關問題