views:

65

answers:

3

In my application i have a threaded commenting system which i call status messages. i have a few models as follows

users(id, name......)

status_messages(id, message,......, user_id)

status_replies(id, message,......, status_message_id, user_id)

is this the right way of writing a commenting system ?

A user can set status messages and his friends and himself can reply to it. if another user replies i would want to pull out his details also.

alt text

+1  A: 

Use the Containable Behavior in the model public $actsAs = array('Containable');

Then, from the User Controller:

$userWithMessagesAndReplies = $this->User->find('first' => array(
        'conditions' => array(/* conditions for finding the user (User.id) */),
        'contain' => array(
            'StatusMessage' => array('StatusReply')
        )
    )
);

This is just an example. Depending on where you do the find, you would change the code above slightly. I would recommend returning the results of the find from a model method instead of a controller so that it is reusable. (I used a controller example for ease of understanding.)

Stephen
i am getting this error Model "User" is not associated with model "User" [CORE\cake\libs\model\behaviors\containable.php, line 363] when i do this $status = $this->User->StatusMessage->find('first', array('contain' => array('StatusMessageReply' => array('User' => array('condition' => array('User.id' => 1 ) ) ))));
Harsha M V
Sounds like you're doing the find from a method in your User model. The above code assumes it's in the controller. For the model, do the same thing, but instead of `$this->User->find` just use `$this->find`
Stephen
Oh, wait... Where are you doing your call from?
Stephen
inside a action in the User COntroller
Harsha M V
+1  A: 

If you want a threaded comment system you should consider using the Tree or MultiTree behavior, with find('threaded').

Adam
You are correct that this could be useful if he decides to use deep threads, but for a single child approach it's a bit too much. From the CakePHP Docs for the Tree Behavior: `For small trees of data, or where the data is only a few levels deep it is simple to add a parent_id field to your database table and use this to keep track of which item is the parent of what.`
Stephen
+1  A: 

I don't think you need 2 tables.

Use a single messages table and create 2 models that both use that table.

I.E table = messages, Models = ProfileMessage, ProfileReply

In that table make sure there is a profile_id, a user_id
and a parent_id. Default all of these to null.

Relate the ProfileMessage as
    belongsTo User, 
    belongsTo Profile,
    hasMany ProfileReply

Relate the ProfileReply as
    belongsTo ProfileMessage using the foreignKey key in the association to make sure you reference the parent_id and profile_id,
    belongsTo User

Then you can just query the ProfileMessage and it should show all of the child ProfileReply objects as if they came from a separate related table. Basically a single level tree structure relates the entries to their parent entries in the same table.

Abba Bryant
i tried to do this. but i get an error telling that profile_reply table not found :(
Harsha M V
You need to set the ProfileReply to use the messages table. http://book.cakephp.org/view/1059/useTable
Abba Bryant
cool got that working :D i am not able to pull the records properly. When the ProfileMEssages are pulled how can i make sure id = pid only these the ProfileMessages. and when the recurvise is increased to pull ProfileReply it also pulls the ProfileMessage under the Reply.
Harsha M V
Once I get off work I will whip up some models and test queries and put them into a pastebin somewhere for you.
Abba Bryant
cool. will wait for it :D
Harsha M V