Loadsys

LoadStr - A Loadsys Blog

A Web Development Company Specializing In Standard-Based Web Design and the CakePHP Framework.

CakePHP Comment Plugin

The goal of every programmer is to write as little code as possible.  It can only be achieved by re-using code that you already wrote.  That is why we all use CakePHP in the first place right?  Fortunately, cake allows us to create plug-ins that we could just drop in into our projects and be done with it.

For quite a few of our projects, especially social applications, we had to add commenting.  In addition, we typically had to allow users to comment on everything (photos, posts, news, articles, etc…). Here is the solution we came up with that worked very well:

Download the Comments Plugin here

1. Create comments table

create table comments (
id int unsigned not null auto_increment,
model varchar(255) not null,
foreign_key int unsigned not null,
comment text not null,
created datetime,
creator_id int unsigned,
modified datetime,
modifier_id int unsigned,
primary key(id),
foreign key(creator_id) references users(id),
foreign key(modifier_id) references users(id)
)Engine=InnoDB;

Please note that this example assumes that you already have users table created.  On how to set creator_id and modifier_id automatically please refer to the previous post by us called “Automagically setting user ID of record creator and modifier in CakePHP 1.2

‘model’ and ‘foreign_key’ field will be used to identify which model the comment belong to.

2. Unzip the plugin code (comments_plugin.zip) into your cake’s plugin directory.

3. Go to the view where you want to attach your comments to. Insert the following line where the comments are suppose to appear:

<?php echo $this->requestAction('/comments/index/<ModelName>/<ModelId>', array('return')); ?>

requestAction? Hmm… You probably were told not to use it ever and that it’s evil. But in actuality it’s not harmful at all if used in moderate amounts ( as like everything else in life…). The moderate use of requestAction and the ease of management of you app will outweigh the slowness cause by the use of it.

And that’s it. You are done.

Please note that this plugin works very nicely with our jQuery Ajax helper

Share This

Tags: , , , , ,

7 Responses to “CakePHP Comment Plugin”

  1. Miles J Says:

    Very nice idea for a plugin, perhaps you can plug in my commentia behavior.

    http://www.milesj.me/resources/script/commentia-behavior


  2. Carlos Says:

    Thanks for the plugin. I’m getting 2 errors using the latest verion of Cake, and I’ve spent 2 days trying to fix it with no luck. Thought may you could point me in the right direction for a solution.

    Notice (8): Array to string conversion [CORE/cake/libs/router.php, line 954]

    array_combine - [internal], line ??
    Router::mapRouteElements() - CORE/cake/libs/router.php, line 954

    Notice (8): Array to string conversion [CORE/cake/libs/router.php, line 1086]

    str_replace - [internal], line ??
    Router::__mapRoute() - CORE/cake/libs/router.php, line 1086

    The errors are occurring after…

    And before the start of the comments input form….
    <form id=”form2042404365″


  3. Carlos Says:

    Sorry, should have known it would have stripped the code…

    The errors are occurring in the class=”comments form” div


  4. Martin Says:

    I experienced the same array to string conversion problem. Finally found a solution:-

    The ajax form call in the add.ctp should read:-

    echo $ajax->form(’add’, ‘post’, array(’model’ => ‘Comment’, ‘update’ => $divId));

    Hope this helps. Very useful plugin, many thanks.


  5. Casmit Says:

    great plugin, saved me a few hours!!


  6. Naka Says:

    I’d like to add “delete” action for comment with ajax.

    Could you help me ? I trid.. but my code skill is not enough…;)

    please help me someone.

    from japan.


  7. Naka Says:

    I have 2 securies question.

    - Can I add new comment as different user_id ?

    if I am “Mike”, Can I post comment as “Jack” ??
    I want to avoid this.

    - Can I delete new comment of different user..
    If I am “Mike”, if I can delete post of “Jack”. this is problem.

    please give me advice.

    from japan. naka.


Leave a Reply

Fill out the following to submit a comment.





Close
E-mail It