CakePHP Schema and Tableless Models
I 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.











March 13th, 2008 at 9:57 am
Thank you! Worked like a charm.
March 14th, 2008 at 11:20 am
thanks a lot, was having this same problem and your suggestion worked perfectly.
April 8th, 2008 at 1:11 pm
Sweet goodness. I tip my hat to you, sir. This is exactly what I needed.
April 16th, 2008 at 5:11 am
good work … thanks
buddy
April 30th, 2008 at 1:01 am
Hey thanx…
you saved my day!!
June 13th, 2008 at 12:48 am
I’m new to Cake PHP.
Thanks for the heads up, it was really helpful.
June 27th, 2008 at 3:04 am
Thanks a lot… it was driving me crazy…

July 15th, 2008 at 1:57 am
Thanks a lot Aaron, you (almost) saved my life !
November 13th, 2008 at 6:23 am
Thanks.