CakePHP Schema and Tableless Models
Thursday, January 31st, 2008I have upgraded to the recent CakePHP 1.2 Beta release, and using a model without a real database table has changed. You need to defined the schema in the model. Schemas exist for models with tables also, so you can this in your model (with a table) to see an example.
pr($this->Model->schema()); exit();
Now, in your model without a real database table you defined the schema like this:
var $_schema = array(
'firstName' => array('type' => 'string',
'length' => 30),
'lastName' => array('type' => 'string',
'length' => 30),
'email' => array('type' => 'string',
'length' => 30),
'msg' => array('type' => 'text')
);
Now you can create validation rules in your model as normal (future post on this once I am confident).
One other note before I defined a schema I was given an “excessive memory” error.
Fatal error: Allowed memory size of 41943040 bytes exhausted (tried to allocate 35 bytes) in XXX/cake/libs/debugger.php on line 414
… when adding $form->create() to my view. This error reports the problem on some line of debugger.php. Once you comment out this line (and maybe a few more it will report) you will see an error…
Warning (512): (FormHelper::create) Unable to use model field data. If you are using a model without a database table, try implementing schema() [CORE/cake/libs/view/helpers/form.php, line 124]
Warning (512): (Model::getColumnType) Unable to locate model field data. If you are using a model without a database table, try implementing schema() [CORE/cake/libs/model/model.php, line 959]
I hope this saves someone the time it took me to solve this issue. Happy Cake’n.