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.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • NewsVine
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis
  • YahooMyWeb

Tags: , , , , ,

9 Responses to “CakePHP Schema and Tableless Models”

  1. Mike Says:

    Thank you! Worked like a charm.

  2. Dave Says:

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

  3. Brad Says:

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

  4. pooja Says:

    good work … thanks
    buddy

  5. Abhinav Varshney Says:

    Hey thanx…

    you saved my day!!

  6. Wayne Khan Says:

    I’m new to Cake PHP.

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

  7. Jacob Says:

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

  8. Fred Says:

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

  9. marius Says:

    Thanks.

Leave a Reply