CakePHP Schema and Tableless Models

10 Comments

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.

Share and Enjoy:
  • Digg
  • NewsVine
  • Reddit
  • Slashdot
  • Technorati
  • TwitThis
  • Yahoo! Buzz

10 Comments (+add yours?)

  1. Mike
    Mar 13, 2008 @ 09:57:27

    Thank you! Worked like a charm.

  2. Dave
    Mar 14, 2008 @ 11:20:15

    thanks a lot, was having this same problem and your suggestion worked perfectly.

  3. Brad
    Apr 08, 2008 @ 13:11:00

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

  4. pooja
    Apr 16, 2008 @ 05:11:02

    good work … thanks
    buddy

  5. Abhinav Varshney
    Apr 30, 2008 @ 01:01:51

    Hey thanx…

    you saved my day!!

  6. Wayne Khan
    Jun 13, 2008 @ 00:48:07

    I’m new to Cake PHP.

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

  7. Jacob
    Jun 27, 2008 @ 03:04:20

    Thanks a lot… it was driving me crazy…
    ;)

  8. Fred
    Jul 15, 2008 @ 01:57:08

    Thanks a lot Aaron, you (almost) saved my life ! ;)

  9. marius
    Nov 13, 2008 @ 06:23:36

    Thanks.

  10. Danilo Cabello
    May 07, 2010 @ 20:21:08

    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!

Leave a Reply