$this->Post->Comment->
...refers to the model structure. $this
denotes this instance of this class, not the current record.
So $this
is the PostsController, $this->Post
is the Post model and $this->Post->Comment
is the Comment model. None of these are referring to a specific record.
$this->Post->id
will only be set if a previous query (in this method) has retrieved an unambiguous result and I only rely on it being set immediately after $this->MyModel->save($data)
otherwise I set it explicitly, like:
$this->MyModel->id = $id;
Personally, I would do it as follows and retrieve all of the required associated data in one statement:
$this->Post->contain(array('Comment')); // If you're using containable, which I recommend. Otherwise just omit this line.
$this->Post->read(null,$id);
Now you will have the Post and its associated comments in an array like this:
Array
(
[Post] => Array
(
[id] => 121
[title] => Blah Blah an More Blah
[text] => When the bar is four deep and the frontline is in witch's hats what can be done?
[created] => 2010-10-23 10:31:01
)
[Comment] => Array
(
[0] => Array
(
[id] => 123
[user_id] => 121
[title] => Alex
[body] => They only need visit the bar once in the entire night.
[created] => 010-10-23 10:31:01
)
[1] => Array
(
[id] => 124
[user_id] => 121
[title] => Ben
[body] => Thanks, Alex, I hadn't thought of that.
[created] => 010-10-23 10:41:01
)
)
)
...and you get at the comments like this:
$comments = $this->data['Comment'];
Everything you want (which you can fine tune in the contain()
call) regarding that post is returned in one handy packet. Do, by the way, read up on Containable behaviour if you haven't already. The sooner you start using it, the easier life will become.