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.

11 comments


  • Mike

    Thank you! Worked like a charm.

    March 13, 2008
  • thanks a lot, was having this same problem and your suggestion worked perfectly.

    March 14, 2008
  • Brad

    Sweet goodness. I tip my hat to you, sir. This is exactly what I needed.

    April 8, 2008
  • pooja

    good work … thanks
    buddy

    April 16, 2008
  • Hey thanx…

    you saved my day!!

    April 30, 2008
  • I’m new to Cake PHP.

    Thanks for the heads up, it was really helpful. :)

    June 13, 2008
  • Thanks a lot… it was driving me crazy…
    ;)

    June 27, 2008
  • Thanks a lot Aaron, you (almost) saved my life ! ;)

    July 15, 2008
  • marius

    Thanks.

    November 13, 2008
  • I am testing under the Cake 1.2.7 Stable version and I think that this was fixed.

    I very happy to not having to fake a table schema on a tableless model when I just want to validate data.

    Thanks for the info!

    May 7, 2010
  • Nehal

    Thanks buddy, its nice that cake have so much helpful functions.

    September 27, 2010

Leave a comment


Name*

Email(will not be published)*

Website

Your comment*

Submit Comment

© Copyright Aaron Thies - Designed by Pexeto