Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7997055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:52:27+00:00 2026-06-04T14:52:27+00:00

I’m using cakephp 2.1 and let me tell you: I just love it. In

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:52:29+00:00Added an answer on June 4, 2026 at 2:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.