2017 © Pedro Peláez
 

yii-extension comment-module

Module that adds comments to your application. You can add comments on any AR Model you like.

image

yiiext/comment-module

Module that adds comments to your application. You can add comments on any AR Model you like.

  • Thursday, May 9, 2013
  • by cebe
  • Repository
  • 7 Watchers
  • 29 Stars
  • 33 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 11 Forks
  • 17 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Comment module

makes every entity of your application commentable. Features:, (*1)

  • Create, Update, Delete comments with ajax
  • Gravatar support
  • define multiple models that can be commented
  • Events raised on new, update, delete
  • more coming soon...

If there is something missing here, or you think one step should be described more detailed, please report it. Thanks!, (*2)

Requirements

Resources

Download

There are two ways to get this extension working:, (*3)

  1. Clone repo:, (*4)

    • Go to your application baseDir (protected in default yii webapp).
    • git clone https://github.com/yiiext/comment-module.git extensions/comment-module
      • If your project is in a git repository you can alternatively add comment-module as a submodule like this:
      • git submodule add https://github.com/yiiext/comment-module.git protected/extensions/comment-module
    • go to new comment-modules base dir and run git submodule update --init to get the gravatar extension that's included.
  2. 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., (*5)

Quickstart

Add module to your application config (optional config values are commented):, (*6)

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):

~~~sql
    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:

~~~sql
    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.

~~~php
 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:

~~~php

comments

renderPartial('comment.views.comment.commentList', array( 'model'=>$model )); ?>

Extending Comment-Module

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., (*7)

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., (*8)

onNewComment

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:, (*9)

  • $event->comment is the ActiveRecord instance of the currently added comment.
  • $event->commentedModel is the model the comment was added to.

Possible use cases:, (*10)

  • Send an E-Mail-Notification

onUpdateComment

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:, (*11)

  • $event->comment is the ActiveRecord instance of the updated comment.

onDeleteComment

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:, (*12)

  • $event->comment is the ActiveRecord instance of the deleted comment.

The Versions

09/05 2013

dev-master

9999999-dev https://github.com/yiiext/comment-module

Module that adds comments to your application. You can add comments on any AR Model you like.

  Sources   Download

BSD-3-Clause

The Requires

 

extension yii comments