Carlos Garcia
Carlos Garcia

Reputation: 359

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.

Update:

Since I needed several sets of flags, I went the $hasAndBelongsToMany way. (allos to add new flags without fixing the code but editing a table (eg. via phpmyadmin).

1.- Made a table/model for each set of flags I need:

2.- In my main model, declared the relation to each:

var $hasAndBelongsToMany = array('Sample','Format','Report','Openend','Dataproce','Prefield');  

3.- In my main Controller, populated an array per table to use:

$openends = $this->Project->Openend->find('list', array(
                    //'order' => 'name'
            ));

4.- And use the arrays in the view:

echo $this->Form->input('Dataproce', array('label' => false, 'type' => 'select', 'multiple' => 'checkbox', 'size' => 2));  

=== Old question starts here; correct answer worked for only one set of flags ===

I'd like to save a string and use it to to magically create a group of checkboxes that belong to a single data field.

I'm in the middle of it already.

my view:

echo $this->Form->input('pr_reports', array('type' => 'select',
                             'multiple' => 'checkbox',
                             'options' => array('0' => 'Interrnal',
                                                '1' => 'Disposition',
                                                '2' => 'Web Disposition',
                                                '3' => 'Marginal',
                                                '4' => 'Custom')))

my controller add method, before saving

        // Used serialize() to convert the array to string
        $dataString = serialize($this->request->data['Project']['pr_reports']);
        $this->request->data['Project']['pr_reports'] = $dataString;

The string is being saved allright (encoded it seems but is ok: a:5:{i:0;s)

My question is how would I go when editing the record in order for the checkboxes to check themselves accordingly? That is, where do I unserialize() the string field and handle this on the edit view?

Is there a better way for this?

Thank you very much for any help.

Carlos García

==== 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.

my fields:

pr_data_format` tinyint(3) unsigned NOT NULL,
pr_report_format` tinyint(3) unsigned NOT NULL,  

my controller

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

my model

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

my view

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

Just can't figure it out, tried:

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

but no use, only one field is updated.

Can you help? We'll use some 4 or 5 flagsets.

Thank you so much.

Carlos

Upvotes: 0

Views: 1229

Answers (1)

mark
mark

Reputation: 21743

a proper way to do this is using a behavior. this keeps your model clean and can be applied to several models simply by putting this in your model:

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.

Upvotes: 1

Related Questions