I have two model. PostModel & CommentModel.
I defined two relation in these classes.
<?php
class PostModel extends AppModel {
var $name = 'Post';
var $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'post_id'
)
);
}
?>
and
<?php
class CommentModel extends AppModel {
var $name = 'Comment';
var $belongsTo = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'post_id'
)
);
}
?>
and in database i have posts table and comments table with post_id column.
In the PostController class when i fetch record with this command :
$this->set('post',$this->Post->findById($id));
and debug $post array i see this array:
array(
'Post' => array(
'id' => '4',
'title' => 'روز اول دانشگاه',
'body' => 'سلام. روز اول بود که اومده بودیم '
'created' => '2012-11-11 21:49:48',
'modified' => null
)
)
I expect that [comment] row be in this array but there isn’t.
I search net an debug my app but i can’t solve this issue.!!!!!
note : cakephp version is 2.2.3 stable
Your models are named incorrectly. PostModel should be Post with a posts table, CommentModel should be Comment with a comments table and a post_id in it. Furthermore, this isn’t working because you’ve told your Comments model to look for a ‘post_id’ on the posts table as a foreign key. This is not correct for a Posts hasMany Comments relationship, and since that column doesn’t exist it’s going to fail. If you follow the convention for Models, you don’t actually need to pass anything into the arrays. The ORM will figure it out.