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
Very nice idea for a plugin, perhaps you can plug in my commentia behavior.
http://www.milesj.me/resources/script/commentia-behavior
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″
Sorry, should have known it would have stripped the code…
The errors are occurring in the class=”comments form” div
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.
great plugin, saved me a few hours!!
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.
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.
Keep up on good working!
Hi,
I’ve been trying your plugin but I’m getting this error when I add the request action to my view
Missing Controller
Error: IndexController could not be found.
Error: Create the class IndexController below in file: app/controllers/index_controller.php
Can you help me? I put the plugin in the plugins directory
Thanks for the code. I was searching something like this and now going to try this comment plugin.
Problem:
Error: IndexController could not be found.
Solution:
Create a route to the plugin:
Router::connect(
‘/comments/:action/*’,
array(
‘plugin’ => ‘comments’,
‘controller’ => ‘comments’
)
);
The plugin is in app/plugins/comments
Hi,
I’ve been trying your plugin but I’m getting this error on submitting the comment:
jQuery(‘#form1786697222’).ajaxSubmit is not a function.
Please help me…
Thanks in advance.
Appreciable Idea, does you have any code about capcha generation code?