我可以在_schema Model字段中创建一个自定义的无表模型? [英] What can I put in _schema Model field to build a custom tableless Model?

查看:135
本文介绍了我可以在_schema Model字段中创建一个自定义的无表模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在食谱中阅读了这个诀窍: http ://book.cakephp.org/2.0/en/models/model-attributes.html#usetable

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

现在我想建立一个自定义模式对于我的模型,但没有这个数组的格式。例如,我不知道应该为bool类型输入什么:boolean或bool?

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"?

如果我想获得一个选择框当我使用$ this-> Form-> input,我应该把什么类型?我应该创建一个hasMany关系(有2个无表模型)?

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)?

推荐答案

文档在这里:http://book.cakephp.org/2.0/en/models/model-attributes.html#schema

这里是联系表单的示例:
http://www.dereuromark.de/2011/12/15/tools-plugin-part-2-contact-form/

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

对于布尔值(tinyint 1):

As for booleans (tinyint 1):

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

TIPP:
如果你想快速找到自己:

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

创建一个表apples和一个Apple模型并添加所有要调试的字段类型
然后调用模式schema(),如下所示:

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.

对于第二部分 - 如果值可以被认为是静态的,我对选择框使用以下ENUM解决方案:
http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/
否则应该使用cookbook或数组数据源中记录的关系。

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.

这篇关于我可以在_schema Model字段中创建一个自定义的无表模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆