CakePHP Queries Model Table Despite Using Table = false

I am creating a contact form to send email to the specified address. I am trying to use CakePHP model validation, and since I do not need a table for the contact model, I set useTable to false in the contact model. However, I get an error in the controller function that is sending. Mistake

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

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

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

I thought everything was fine with the CakePHP map. Why am I wrong?

+6
cakephp
source share
6 answers

Edit : see this answer (and comment ) for CakePHP 2.x (the model file should be called Contact.php )


CakePHP 1.x. Make sure your model file is called Contact.php (lowercase). If this is not the case, CakePHP will not find your model and will instead create an "autoModel" at run time called Contact , which uses the contacts table.

+7
source share

This is the best search result, but the information is out of date, I think.

In CakePHP 2.0+, you need to set $useTable = false; in the model, the model name uses the correct value (therefore it should be Contact , not Contact , as suggested), and the controller should have $uses = 'Contact'; or $uses = array('Contact'); or the cake generates default model properties and tries to load a table that does not exist. Therefore, both of these things must be installed in order for them to work.

+4
source share

If memory is used, you are not actually setting your model:

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

In your code, the model does not actually populate when you try to validate it.

+2
source share

If you are using a model without a table, you also need to install a schema, e.g.

 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') ); } 
+2
source share

Two things have changed for me: changing the name of my model file to Contact.php (instead of ContactModel.php) and commenting var $uses = 'Contact'; in my ContactController.php.

In addition, many contact form tutorials are for earlier versions of CakePHP. Be sure to use the correct form input structure. Here is a look at mine in Cake 2.1:

 <?php echo $this->Form->create('Contact'); echo $this->Form->inputs(); echo $this->Form->end('Send'); ?> 
+2
source share

If this helps someone, I found that if I

 var $uses = 'ModelName'; 

in my controller, it will override useTable. Remove it if you do not need it.

+1
source share

All Articles