makes every entity of your application commentable. Features:
If there is something missing here, or you think one step should be described more detailed, please report it. Thanks!
There are two ways to get this extension working:
Clone repo:
protected in default yii webapp).git clone https://github.com/yiiext/comment-module.git extensions/comment-module
git submodule add https://github.com/yiiext/comment-module.git protected/extensions/comment-modulegit submodule update --init to get the gravatar extension that's included.Download latest release and put all the files into
extensions/comment-module under your application baseDir (protected in default yii webapp).
To be able to use Gravatar support you have to copy YiiGravatar.php
into extensions/comment-module/extensions/gravatar.
Add module to your application config (optional config values are commented):
// ... 'modules'=>array( // ... 'comment'=>array( 'class'=>'ext.comment-module.CommentModule', 'commentableModels'=>array( // define commentable Models here (key is an alias that must be lower case, value is the model class name) 'post'=>'Post' ), // set this to the class name of the model that represents your users 'userModelClass'=>'User', // set this to the username attribute of User model class 'userNameAttribute'=>'username', // set this to the email attribute of User model class 'userEmailAttribute'=>'email', // you can set controller filters that will be added to the comment controller {@see CController::filters()} // 'controllerFilters'=>array(), // you can set accessRules that will be added to the comment controller {@see CController::accessRules()} // 'controllerAccessRules'=>array(), // you can extend comment class and use your extended one, set path alias here // 'commentModelClass'=>'comment.models.Comment', ), // ... ), // ...
Create database tables: You can use the database migration provieded by this extension or create a table (example for mysql):
CREATE TABLE IF NOT EXISTS `comments` ( `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, `message` text COLLATE utf8_unicode_ci, `userId` int(11) UNSIGNED DEFAULT NULL, `createDate` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `fk_comments_userId` (`userId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
You might also want to add a foreign key for userId column that references you user tables pk.
Create a database table for every commentable Model relation:
CREATE TABLE IF NOT EXISTS `posts_comments_nm` ( `postId` int(11) UNSIGNED NOT NULL, `commentId` int(11) UNSIGNED NOT NULL, PRIMARY KEY (`postId`,`commentId`), KEY `fk_posts_comments_comments` (`commentId`), KEY `fk_posts_comments_posts` (`postId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
You might want to add foreign keys here too.
Add commentable behavior to all Models you want to be commented.
// ... public function behaviors() { return array( 'commentable' => array( 'class' => 'ext.comment-module.behaviors.CommentableBehavior', // name of the table created in last step 'mapTable' => 'posts_comments_nm', // name of column to related model id in mapTable 'mapRelatedColumn' => 'postId' ), ); }
Finally add comments to your view template of the commentable model:
<h1>comments</h1> <?php $this->renderPartial('comment.views.comment.commentList', array( 'model'=>$model ));
Comment module raises events to which you can attach event handlers to handle them. See The Definitive Guide to Yii on how to do this.
You can also attach behaviors
to CommentModule by setting 'behaviors'=>array(/* ... */) in the module config described above.
See CModule::behaviors on how to add behaviors to a module.
This event is raised when a new comment has been saved.
The following attributes are available on the $event given as the first parameter to the event handler:
$event->comment is the ActiveRecord instance of the currently added comment.$event->commentedModel is the model the comment was added to.Possible use cases:
This event is raised when a user edited a comment.
The following attributes are available on the $event given as the first parameter to the event handler:
$event->comment is the ActiveRecord instance of the updated comment.This event is raised when a user deleted a comment.
The following attributes are available on the $event given as the first parameter to the event handler:
$event->comment is the ActiveRecord instance of the deleted comment.
Total 20 comments
thanks for this awesome extension. a small suggestion:
Currently logged in users are being able to post empty comments as well. Some sort of error message would have been better
thanks again, using this in my first yii app and its working great ! :)
Please read all of the comments. you also have to add this to your Comment.php model
public function getUserImg() { return $this->user->filename; }
Also, please read this post i made too on how to upload files to your db for users User widget this is where the filename comes from.
In the section below where it has yii gravitar widget in _view.php I replace it with this.
EDIT: There is a problem when adding/deleting comments... for example when it shows 1 of 15 comments it doesn't update until you refresh the page. Also, if you are using pagination the comment goes below the pager instead of under the last comment until you refresh the page. Anyone know fixes to these?
I changed the styles (see screen shot at the bottom) I think it looks a lot more professional. This also add an edit link to the comments if the user is admin and it is not the admin's post.
Please Note: the submit button style will change site wide you can change it to be just for the comment if you like. Also, I moved my styles to my main css file. however, if you don't do so you may have to clear the site cache to see changes (you can manually delete them by going to your webroot/assets/ (assets folder is at the same level as protected folder) and deleting everything in there.)
CSS
views/_form
views/_view
views/commentList
New Style
Default Style
This should get rid of your No Scope Error. All it is saying is that you don't have $comment defined. So you will have to define it...
In your main.php under the section below add your commentable models names there.
i.e. I have a models that I added this too named Products and one called Companies.
Also, in the database schema I just left out the unsigned part because my user table doesn't use it and you won't be able to create a FK relationship if they aren't the same.
You should also add FKs when adding your comment tables. If you don't the comments won't delete out of the table. It will only delete out of the comment table.
Here is an example of what I used.
No scope defined in CommentModule for commentable Model Request
I followed your instructions. but I found the error. I changed 'userModelClass'=>'User', to 'userModelClass'=>'Users', and userId at comments tbl is int(11) unsigned null, also same as the id of Users tbl. How could I solve this problem?
@CeBe,
I did create that table
If that's what you were referring to, but still not sure how the module knows the "context" or owner of the post_id.
Ended up editing the above table and adding parent_model and made the appropriate changes to the module model.
Didn't know if you had built the ability into the module to begin with.
is there a way to create pagination?...or the comment list is getting too long...just like the comments in this page...=.=
@w00tw00t111 do you have a relation table created for every model that is commentable? All you have to do is described here:
https://github.com/yiiext/comment-module#quickstart
Also,
for others who are using table prefixes, I found the following was necessary in Comment.php:
Might be slightly confused about this Cebe, but installed it and it's working great.
However, how is the itemview supposed to know which model the comment is related to.
For instance, let's say you have a question with an id of 1 and an answer with a id of 1.
If both models are commentable (question && answer) and each one has a comment added, then when the view searches for id of 1, won't it pull both comments for each individual model?
This is the case I am having currently. In modules that I've designed for my app that are reusable by multiple models I have had to add a column "parent_model" to store the model class that this particular item is for. So in the question/answer example it would be stored as follows: comment_id: 1, parent_model: Question, parent_id: 1, message: test, createDate: NOW() and comment_id: 2, parent_model: Answer, parent_id: 1, message: test2, createDate: NOW()
Hopefully, that makes sense. Am I doing something wrong? Is this functionality already built in?
Thanks CeBe!
Thanks..That works!
Amazing extension...save me tons of time
Cheers Jiaming
@jzhong5: how about get_class?
Hi, I am using this module to send email notification and also using it in other models.
However, I just send email in one Model, so I don't want to messed things up...
I know $event->commentedModel will give me the specific values in the model...
But How can i get the model's name?
For example ,I use this ext in Questions Model, how can I get "Questions" instead of a specific id or name belongs to 'Questions'..?
Because if i can get this, I can only send email notification in the specific model....
Best, jiaming
THANKS!! Works like a charm now...
THANKS FOR YOUR HELP!!!
To attach eventhandlers to modules events, you can configure a callback inside your main config, or if you have PHP 5.3 or higher you can even add an anonymous function there:
Any hints about how to trigger events in this extensions...?
I want to send email to the admin when specific model was commented....
I tried to add some trigger codes in the Comment Controller but not works...
Any ideas??
Thanks so much!
@p0rsche: you have to refresh the model after saving with CTimeStampBehavior like it is discussed here.
I've added my own field to CTimestampBehavior behavior (createAttribute), but when I'm trying to get comment added date in onNewComment event, I've got CDBexpression instead of string date representation... How can I get the string of date?
@CeBe It works now. Thanks so much for your amazing extensions!!!
@jzhong5 just do the database column definition for userId as it is required for your db as long as it is an integer it does not matter if it is unsigned or not, or BIGINT or whatever.
Also make sure you have commentableModels configured in your module.
Leave a comment
Please login to leave your comment.