views:

46

answers:

2

I am trying get all commments relateed to my current post, I am doing it as following:

$this->Post->Comment->find('all', array('conditions' => array('post_id' => $id)));

but in my opinion, it is a little uncomfortably. Why I should give post_id? Isn't it obvious that I want Comment related to current Post?

+1  A: 

$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.

Leo
Using containable is redundant here. You do not need it. You do not need to waste the memory to run unnecessary classes.
bancer
Not necessarily - I included it as a prompt to use it and explore its use. The Post might be associated with users, images or whatever, but we're only looking for comments here. Using it like this will restrict the return Post + comments. Note that I said omit it if you're not using it. If you are, then the class is already loaded so wasting memory is irrelevant.
Leo
The potential for avoiding wasted memory and processing time by using containable is far more significant than the overhead of loading the class.
Leo
A: 

If you do not specify the conditions ($this->Post->Comment->find('all');) you will get all comments with their related records.

It is advisable to specify the model name in the conditions: $this->Post->Comment->find('all', array('conditions' => array('Comment.post_id' => $id)));. This way you will get the comments for the specific post and all associated (to the comments) data.

If you do not like 'conditions' array you need to specify id explicitly as Leo mentioned (from Post controller):

$this->Post->id = $id;
$this->Post->read();

This way you will get the post and all data associated to it.

Notice that in the first method you retrieve all data associated to the comments and in the second method - all data associated to the post.

bancer
Thx, but how could I now display comments on view?
John X
`debug($data);` in the view. The comments should be in `$data['Comment'];`.
bancer