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:
- Removed the user_id field in ‘posts’ (to simplify the example)
- 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:
- Bake a new application
- Connect to database
- Paste files from zip example to replace the newly baked files
- Create database tables using the SQL




August 24th, 2007 at
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
August 24th, 2007 at
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.
August 27th, 2007 at
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…
August 28th, 2007 at
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…
September 12th, 2007 at
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.
September 12th, 2007 at
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.