English 中文(简体)
用于创建复选框的 cakephp 字符串字段
原标题:cakephp string field used to create checkboxes

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

但无用,只更新了一个字段。

需要帮忙吗?

非常感谢

卡洛斯·卡洛斯

最佳回答

这样做的正确方式是使用一种行为。 这可以保持你的模型干净, 并且可以应用到几个模型中, 只需将它放入模型 :

public $actsAs = array( MyBehavior );

now, for serializing I use my Jsonable Behavior: http://www.dereuromark.de/2011/07/05/introducing-two-cakephp-behaviors/ it basically makes your input array a storable string on save and the string back to an array on read. you could easily adjust this to your needs.

BUT for what you want to do with multiple checkboxes there is even a better thing - bitmasks. I developed a so called Bitmasked behavior - you would need to use 1,2,4,8,... but other than that its the same thing: http://www.dereuromark.de/2012/02/26/bitmasked-using-bitmasks-in-cakephp/ I use it exactly for the same thing.

问题回答

暂无回答




相关问题
WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Convert a 2D array index into a 1D index

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making a GUI for it and I want it ...

Convert an array of integers for use in a SQL "IN" clause

Surely there is a framework method that given an array of integers, strings etc converts them into a list that can be used in a SQL "IN" clause? e.g. int[] values = {1,2,3}; would go to "(1,2,3)"

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

Best practice of big javascript objects

sry for this imprecise topic name. I am querying a dataset a lot of times so using ajax request would end up in tons of http requests. For this reason I decided to use the json encode method to ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签