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?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?