What can I put in _schema Model field to build a custom tableless Model?

守給你的承諾、 提交于 2019-12-08 13:57:18

问题


I already read this "trick" in the cookbook: http://book.cakephp.org/2.0/en/models/model-attributes.html#usetable

Now I would like to build a custom schema for my model, but there isn't the format for this array. For example, I don't know what should I put for a bool type: "boolean" or "bool"?

If I want to obtain a "select box" when I use $this->Form->input, what type should I put? Should I create a hasMany relationship (with 2 tableless models)?


回答1:


the docs are here: http://book.cakephp.org/2.0/en/models/model-attributes.html#schema

here is an example for a contact form: http://www.dereuromark.de/2011/12/15/tools-plugin-part-2-contact-form/

As for booleans (tinyint 1):

protected $_schema = array(
   'status' => array(
       'type' => 'boolean',
       'length' => 1,
       'default' => 0,
       'null' => false,
       'comment' => 'some optional comment'
   ),   
);

TIPP: if you want a quick way to find this out yourself:

create a table "apples" and an Apple model and add all types of fields you want to debug then call the model schema() like so:

debug($this->Apple->schema());

this is how I confirmed the above.

And for the second part - I use the following ENUM solution for select boxes if the values can be considered kind of "static": http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/ otherwise you should use relations as documented in the cookbook or the array datasource.



来源:https://stackoverflow.com/questions/10049567/what-can-i-put-in-schema-model-field-to-build-a-custom-tableless-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!