Using HABTM relationships in cakephp plugins with unique set to false

Posted by Dean on Stack Overflow See other posts from Stack Overflow or by Dean
Published on 2010-03-24T10:28:52Z Indexed on 2010/03/24 10:33 UTC
Read the original article Hit count: 268

Filed under:
|

I am working on a plugin for our CakePHP CMS that will handle blogs. When getting to the tags I needed to set the HABTM relationship to unique = false to be able add tags to a post without having to reset them all.

The BlogPost model looks like this

class BlogPost extends AppModel {
    var $name = 'BlogPost';
    var $actsAs = array('Core.WhoDidIt', 'Containable');
    var $hasMany = array('Blog.BlogPostComment');
    var $hasAndBelongsToMany = array('Blog.BlogTag' => array('unique' => false), 'Blog.BlogCategory');
}

The BlogTag model looks like this

class BlogTag extends AppModel {
    var $name = 'BlogTag';
    var $actsAs = array('Containable');
    var $hasAndBelongsToMany = array('Blog.BlogPost');
}

The SQL error I am getting when I have the unique => true setting in the HABTM relationship between the BlogPost and BlogTag is

Query: SELECT `Blog`.`BlogTag`.`id`, `Blog`.`BlogTag`.`name`, `Blog`.`BlogTag`.`slug`, `Blog`.`BlogTag`.`created_by`, `Blog`.`BlogTag`.`modified_by`, `Blog`.`BlogTag`.`created`, `Blog`.`BlogTag`.`modified`, `BlogPostsBlogTag`.`blog_post_id`, `BlogPostsBlogTag`.`blog_tag_id` FROM `blog_tags` AS `Blog`.`BlogTag` JOIN `blog_posts_blog_tags` AS `BlogPostsBlogTag` ON (`BlogPostsBlogTag`.`blog_post_id` = 4 AND `BlogPostsBlogTag`.`blog_tag_id` = `Blog`.`BlogTag`.`id`)

As you can see it is trying to set the blog_tags table to 'Blog'.'BlogTag. which isn't a valid MySQL name.

When I remove the unique => true from the relationship it all works find and I can save one tag but when adding another it just erases the first one and puts the new one in its place.

Does anyone have any ideas? is it a bug or am I just missing something?

Cheers, Dean

© Stack Overflow or respective owner

Related posts about cakephp

Related posts about plugins