tags:

views:

631

answers:

3

I'm creating a contact form to send an email to a specified address. I'm trying to utilize CakePHP model validations and since I don't need a table for the contact model, I've set useTable to false in the contact model. Yet I'm getting an error in the controller function that does the sending. The error is

Missing Database Table Error: Database table contacts for model Contact was not found.

pointing to the line that makes the first call to $this->Contact:

$this->Contact->validates( $this->data );

I thought this was all good to go with the CakePHP framework. Why am I wrong?

+2  A: 

If memory serves, you're not actually setting your model:

$this->Contact->set( $this->data );
$this->Contact->validates();

In your code, the model isn't actually populated when you try to validate it.

Rob Wilkerson
+2  A: 

Verify that your model file is called contact.php (lowercase). If it is not, CakePHP won't find your model and and will instead create an "autoModel" on runtime called Contact which uses the contacts table.

deizel
wowsers. i went over that a few times myself, not until now realizing i named it contact.ctp out of view creating habit. i feel like a goober.
Adam
hehe, we've all been there. one that got me pretty good once was `actsAs` vs `actAs`, hours wasted.
deizel
+1  A: 

If you are using a model without a table you also need to set a schema eg

class Contact extends AppModel {
    var $name = 'Contact';
    var $useTable = false;
    var $_schema = array(
     'name' => array('type' => 'string', 'length' => 255),
     'email' => array('type' => 'string', 'length' => 255),
     'message' => array('type' => 'text')
    );
}
Mathew Attlee