HABTM / Checkbox working example

Reading the CakePHP group messages, a lot of people seem to run into problems with creating HABTM relationships and changing from the default multipleSelect to using a series of checkboxes. In an app I was building I ran into a brick wall where I just wasn’t able to find out waht was wrong so I just decided to bake a quick example that I could use as a reference.

This is for Cake 1.1.x.x

I used a slightly modified copy of the SQL from the manual and used  Josh McFarren’s checkbox helper.

--
-- Table structure for table `posts`
--

CREATE TABLE `posts` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `title` varchar(50) default NULL,
  `body` text,
  `created` datetime default NULL,
  `modified` datetime default NULL,
  `status` tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;

-- --------------------------------------------------------

--
-- Table structure for table `posts_tags`
--

CREATE TABLE `posts_tags` (
  `post_id` int(10) unsigned NOT NULL default '0',
  `tag_id` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`post_id`,`tag_id`)
) TYPE=MyISAM;

-- --------------------------------------------------------

--
-- Table structure for table `tags`
--

CREATE TABLE `tags` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(100) default NULL,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;

I have made two changes to the SQL above:

  1. Removed the user_id field in ‘posts’ (to simplify the example)
  2. Changed ‘tag’ to ‘name’ in ‘tags’ – I changed this because generateList was not working and the $tags array geneated for the view did not contain the tag name ‘tag’ – only the ‘tag_id’ value.

You can download my working example, it contains all of the files that I create in bake and the files added and changed to use the checkboxHelper. To install the example:

  1. Bake a new application
  2. Connect to database
  3. Paste files from zip example to replace the newly baked files
  4. Create database tables using the SQL

HABTM_checkbox_example.zip

6 thoughts to “HABTM / Checkbox working example”

  1. Thanks!! It is really hard to piece together this stuff from the website. I get one error

    Warning: file_put_contents(e:\apache\habtm\tmp\cache\models\default_tags) [function.file-put-contents]: failed to open stream: No such file or directory in E:\apache\cake\basics.php on line 935

    but this could be a config issue on my side. Otherwise, it works great. Thanks!

    Dean

  2. Hi

    Glad it’s been of use. I had a quick thought that it could be something along the lines of not having copied / modified your the tags.ini.php to /app/config/ from /cake/config/… but I don’t think that is it on reflection.

    Sometimes it is really hard to figure out what is going on I guess because the framework is so big.

  3. Also, I found that you now have to declare variables a little more carefully. So there are a couple of places where I had to change:

    $this->set(‘selectedTags’, null);

    to

    $this->set(‘selectedTags’, array());

    or you get an error that habtm is getting the wrong datatype in the second position…

  4. I think that I’m so late here, but if you can read this maybe I can help you a bit.

    I’m also a new Cake developer. Now working on a News Helper.

    You said:
    Changed ‘tag’ to ‘name’ in ‘tags’ – I changed this because generateList was not working and the $tags array geneated for the view did not contain the tag name ‘tag’ – only the ‘tag_id’ value.

    Well, you don’t have to do it. In the tag’s model, add that line:
    //just under var $name = ‘Tag’;
    var $displayField = ‘name’;

    Then, in the generateList, you’ll get the name instead the id.

    Sorry for my inglish and hope I help…

  5. Hi Pinholecam

    Thanks for pointing that out. I hope it is something I would notice now, but when I was doing this some of the finer points of model associations were just lost on me.

  6. Hi Dean

    Are you using 1.2?

    Having said that – been meaning to look at this all again – there are instances when it does error.

Comments are closed.