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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
--
-- 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 Responses to “HABTM / Checkbox working example”

  1. Dean

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

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

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

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

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

    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.