tags:

views:

300

answers:

5

I'm just learning CakePHP, my simple app links two tables with a hasAndBelongsToMany relationship. I was expecting that the scaffolding would enable me to associate posts with tags but it doesn't.

Here is the code:

SQL

CREATE TABLE  `posts` (
`id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`content` TEXT,
PRIMARY KEY (  `id` )
);

CREATE TABLE  `tags` (
`id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 100 ) DEFAULT NULL ,
PRIMARY KEY (  `id` )
);

CREATE TABLE  `posts_tags` (
`id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`post_id` INT( 11 ) UNSIGNED DEFAULT NULL ,
`tag_id` INT( 11 ) UNSIGNED DEFAULT NULL ,
PRIMARY KEY (  `id` )
);

contents of models folder:

post.php

class Post extends AppModel {
var $name = 'Post';
var $hasAndBelongsToMany = array('Tag');
}

tag.php

class Tag extends AppModel {
var $name = 'Tag';
var $hasAndBelongsToMany = array('Post');
}

contents of controllers folder

posts_controller.php

class PostsController extends AppController {
var $name = 'Posts';
var $scaffold;
}

tags_controller.php

class TagsController extends AppController {
var $name = 'Tags';
var $scaffold;
}

In the app, I can create/edit/delete a post or a tag, but there is no way to link them.

edit: In my scaffolded views there are no multiple selects to associate tags to posts etc. I'm using v 1.2.4.8284.

A: 

In your scaffolded views there should be a multiple select for you to associate tags to posts and posts to tags. Then when you view the post, you see related tags, and viewing the tag shows related views. I used the code you provided and was able to produce these results. Are you no seeing the same thing?

Jesse Kochis
A: 

This was a bug in version 1.2.4 - it has been fixed in version 1.2.5

Richard
A: 

ya, there is some problem with version 1.2.4. I faced similar problem too.

Learner
A: 

Has anyone verified this? I have been pulling my hair out with 1.2.4.8284 and HABTM. Is it fixed in 1.3? Or should I downgrade to 1.2.3?

It has been raised and accepted as a bug in 1.2.4.8284 http://code.cakephp.org/tickets/view/48
Richard
A: 

Yes, I can confirm that I had the same problem in 1.2.4.8284 -- i.e. All the HABTM relationships were missing from the scaffolded forms.

I reverted to 1.2.3.8166 and it's back to normal, as it should be.