0

I am using cakephp since few months, and I have a problem today that I don't know why it is not working as always.

I've got three table:

posts comments posts_comments

In the models, I set :

Comment Model:

var $hasAndBelongsToMany = array('Post');

Post Model

var $hasAndBelongsToMany = array('Comment');

Then in my controller, for example the PostController:

$this->set('comments', $this->Post->Comment->find('list', array('order' => 'Comment.id')));

And then in my Post View I have :

<?php
echo $this->Form->create('Post');
echo $this->Form->input('name', array('label' => 'Name', 'maxlength' => '100'));
echo $this->Form->input('Comment.Comment', array('label' => 'New Comment', 'type' => 'select', 'multiple' => true));
echo $this->Form->submit('Add', array('class' => 'button'));
?>

In my others projects it always worked ! I always had my "Comment" displayed on the list, but here, I don't know why it is not working, nothing is displayed, did I forgot something ?

Regards,

4m0ni4c.

NH3
  • 189
  • 2
  • 12
  • 1
    You probably don’t want to use a HABTM relationship for posts and comments as a comment can’t belong to many posts, it usually just belongs to just one post, where a post can have many comments. Therefore `Post` should `haveMany` `Comment`, and `Comment` should `belongTo` `Post`. – Martin Bean Mar 05 '13 at 09:07
  • I chose this example to illustrate my problems, I my case, in really need the HABTM relationship :) – NH3 Mar 05 '13 at 09:24
  • My main problem is that I usually don't need to specify the "options" value in my others input, but here, I don't really know why, if I don't specify this variable, I can't display my Comment :/ – NH3 Mar 05 '13 at 09:51
  • We might be able to better diagnose if you presented us with the actual problem. – Martin Bean Mar 05 '13 at 10:36
  • The actual problem is that I can't display my comment in the list that I set via the input "Comment.Comment" without setting up the "options" variable. – NH3 Mar 05 '13 at 11:08
  • What do you mean by “options” variable? You don’t mention it in your question, just in the comments. We need more complete code if you expect anyone to answer. – Martin Bean Mar 05 '13 at 11:21

1 Answers1

0

I don't know if you solved it yet, but according to Cake convention, the HABTM tables should be named alphabeticaly for them to work magically.

This new join table’s name needs to include the names of both models involved, in alphabetical order, and separated with an underscore ( _ ). The contents of the table should be two fields, each foreign keys (which should be integers) pointing to both of the primary keys of the involved models

So your posts_comments table should be comments_posts, unless you change the association directly on Post and Comment model.

Nunser
  • 4,512
  • 8
  • 25
  • 37