comment-module

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

  • 所有者: yiiext/comment-module
  • 平台:
  • 许可证:
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

Comment module

makes every entity of your application commentable.
Features:

  • 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!

Requirements

Resources

Download

There are two ways to get this extension working:

  1. Clone repo:

    • 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.

Quickstart

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

<?php
    // ...
    '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.

<?php
    // ...
    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
)); ?>

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.

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.

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:

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

Possible use cases:

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

  • $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:

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

主要指标

概览
名称与所有者yiiext/comment-module
主编程语言PHP
编程语言PHP (语言数: 1)
平台
许可证
所有者活动
创建于2011-12-13 04:06:04
推送于2013-05-09 15:14:43
最后一次提交2013-05-09 18:14:42
发布数2
最新版本名称0.6.0 (发布于 2011-12-22 05:58:52)
第一版名称0.5.0 (发布于 2011-12-19 03:05:57)
用户参与
星数29
关注者数8
派生数11
提交数32
已启用问题?
问题数25
打开的问题数17
拉请求数0
打开的拉请求数0
关闭的拉请求数0
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?