Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a form with no relation to a model in cakePHP

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?

like image 351
Bird87 ZA Avatar asked Dec 15 '22 13:12

Bird87 ZA


1 Answers

Instead of null, use false:

echo $this->Form->create( false, array('type' => 'file')); ?>
like image 69
Alvaro Avatar answered Feb 13 '23 14:02

Alvaro