I m using cakephp 2.1 and let me tell you: I just love it.
In my form I have a field that can have multiple answers (checkboxes).
I don t want to create a database field for each option, nor use an HABTM.
更新 :
由于我需要几套国旗, 我走上了美元和Belongs To Many 的方式。 (可以添加新国旗,
1. 为我需要的每套旗帜制作一张表格/模型:
2. 在我的主要模式中,我宣布了与下列每一种情况的关系:
var $hasAndBelongsToMany = array( Sample , Format , Report , Openend , Dataproce , Prefield );
3. 在我的首席主计长中,每张表格各组人,使用:
$openends = $this->Project->Openend->find( list , array(
// order => name
));
4. 并使用视图中的数组:
echo $this->Form->input( Dataproce , array( label => false, type => select , multiple => checkbox , size => 2));
旧问题从这里开始;正确答案只对一套旗帜有效
我想保存一个字符串,然后用它来神奇地创建一组属于单一数据字段的复选框。
我已经在忙了
我的看法是:
echo $this->Form->input( pr_reports , array( type => select ,
multiple => checkbox ,
options => array( 0 => Interrnal ,
1 => Disposition ,
2 => Web Disposition ,
3 => Marginal ,
4 => Custom )))
我的控制器在保存前添加方法
// Used serialize() to convert the array to string
$dataString = serialize($this->request->data[ Project ][ pr_reports ]);
$this->request->data[ Project ][ pr_reports ] = $dataString;
字符串正在全部保存中( 似乎编码为它, 但可以 : a: 5: {i: 0; s)
我的问题是,在编辑记录以便复选框进行相应检查时,我该如何去编辑记录? 也就是说,我该在哪里解密字符串字段并在编辑视图中处理该字段?
有没有更好的办法?
非常感谢你的帮助
卡洛斯·加西亚
==== After solution, troubles to have more than one field with a different set of flags
data is only saved for one field, ignoring the other ====
Hello;
For one field on the table it works just fine as noted;
I m having a hard time using another field (separate set of flags).
It seems only one behaviour is attached; I was wondering if I should attach them differently.
我的字段 :
pr_data_format` tinyint(3) unsigned NOT NULL,
pr_report_format` tinyint(3) unsigned NOT NULL,
我的控制器
$this->Project->Behaviors->attach( Bitmasked , array( mappedField => pr_data_formats , bits => Project::pr_data_formats ));
$this->Project->Behaviors->attach( Bitmasked , array( mappedField => pr_report_formats , bits => Project::pr_report_formats ));
我的型号
const STATUS_ASCII = 1;
const STATUS_SPSS = 2;
const STATUS_EXCEL = 4;
const STATUS_CUSTOM = 8;
public static function pr_data_formats($value = null) {
$options = array(
self::STATUS_ASCII => __( ASCIId ),
self::STATUS_SPSS => __( SPSSBd ),
self::STATUS_EXCEL => __( Exceld ),
self::STATUS_CUSTOM => __( Customd ),
);
return parent::enum($value, $options);
}
const REP_ASCII = 1;
const REP_SPSS = 2;
const REP_EXCEL = 4;
const REP_CUSTOM = 8;
public static function pr_report_formats($value = null) {
$options = array(
self::REP_ASCII => __( ASCIIr ),
self::REP_SPSS => __( SPSSBr ),
self::REP_EXCEL => __( Excelr ),
self::REP_CUSTOM => __( Customr ),
);
return parent::enum($value, $options);
}
我的看法是:
echo $this->Form->input( pr_data_formats , array( options => Project::pr_data_formats(), multiple => checkbox ));
echo $this->Form->input( pr_report_formats , array( options => Project::pr_report_formats(), multiple => checkbox ));
只是无法想通,尝试过:
$this->Project->Behaviors->attach( Bitmasked , array( mappedField => pr_report_formats , bits => Project::pr_report_formats ), array( mappedField => pr_data_formats , bits => Project::pr_data_formats ));
但无用,只更新了一个字段。
需要帮忙吗?
非常感谢
卡洛斯·卡洛斯