4

I have a form that a user selects a document in. This document is then saved to the webroot folder by the controller.

However, before it even shows the form, it gives me an error:

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

Which is correct, there is no database table for office_layouts. But it's not needed. That's why there is no table. The form is just uploaded to the server.

I have read through creating forms, and have tried the following:

// 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')); ?>

As well as chaning the null to the controller name (in this case OfficeLayout). I have also removed all parameters (thus creating it like so: $this->Form->create())

In my controller I have the following:

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

This action get's called with: domain/office_layouts/upload/branch.

Whenever I remove the $this->Form->create() line, it shows the upload view, but then obviously doesn't submit.

So my question in this case is, how can I create a form, without it looking for a table in the database?

tereško
  • 58,060
  • 25
  • 98
  • 150
Bird87 ZA
  • 2,313
  • 8
  • 36
  • 69

2 Answers2

12

Instead of null, use false:

echo $this->Form->create( false, array('type' => 'file')); ?>
Alvaro
  • 40,778
  • 30
  • 164
  • 336
0

Fixed it by creating a Model file and just putting this in there:

class OfficeLayout extends AppModel {
    var $useTable = false;
}
Bird87 ZA
  • 2,313
  • 8
  • 36
  • 69
  • There's no need to create an empty model. Just use false when creating the form. More simple :) I added an answer. – Alvaro Mar 01 '13 at 12:37