yiiext/comment-module

模块,用于在应用程序中添加评论。您可以在喜欢的任何 AR 模型上添加评论。

安装: 41

依赖: 0

建议者: 0

安全: 0

星星: 29

关注者: 9

分支: 11

开放问题: 17

类型:yii-extension

dev-master 2013-05-09 15:14 UTC

This package is auto-updated.

Last update: 2024-09-05 18:18:29 UTC


README

使应用程序中的每个实体都支持评论。功能

  • 使用 Ajax 创建、更新、删除评论
  • Gravatar 支持
  • 定义多个可评论的模型
  • 在新建、更新、删除时触发事件
  • 更多功能即将推出...

如果这里缺少某些内容,或者您认为某个步骤需要更详细的描述,请报告问题。谢谢!

要求

  • 已测试与 Yii 1.1.8 兼容,应在早期版本中工作
  • yii-gravatar 扩展以支持 Gravatar

资源

下载

有两种方法可以使此扩展正常工作

  1. 克隆仓库

    • 转到您的应用程序基础目录(默认的 yii 网页应用程序中的 protected)。
    • git clone https://github.com/yiiext/comment-module.git extensions/comment-module
      • 如果您的项目在 git 仓库中,您可以将 comment-module 作为子模块添加,如下所示
      • git submodule add https://github.com/yiiext/comment-module.git protected/extensions/comment-module
    • 转到新的 comment-modules 基础目录,并运行 git submodule update --init 以获取包含的 gravatar 扩展。
  2. 下载最新版本,并将所有文件放入应用程序基础目录(默认的 yii 网页应用程序中的 protected)下的 extensions/comment-module。为了能够使用 Gravatar 支持,您必须将 YiiGravatar.php 复制到 extensions/comment-module/extensions/gravatar

快速入门

将模块添加到应用程序配置中(可选配置值已注释)

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

创建数据库表:您可以使用此扩展提供的数据库迁移或创建一个表(以下为 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;

您可能还需要为 userId 列添加外键,该外键引用您的用户表 pk。

为每个可评论的模型关系创建数据库表

    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;

您可能还想在这里添加外键。

将可评论行为添加到所有想要添加评论的模型

<?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'
            ),
       );
    }

最后,将评论添加到可评论模型的视图模板

<h1>comments</h1>

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

扩展 Comment-Module

Comment 模块会触发事件,您可以在事件处理器中附加事件处理器来处理它们。有关如何做到这一点的说明,请参阅The Definitive Guide to Yii

您还可以通过在上面的模块配置中设置 'behaviors'=>array(/* ... */) 来将 behaviors 附加到 CommentModule。有关如何向模块添加行为的说明,请参阅CModule::behaviors

onNewComment

当保存新评论时,会触发此事件。以下属性在作为事件处理器第一个参数提供的 $event 上可用

  • $event->comment 是当前添加的评论的 ActiveRecord 实例。
  • $event->commentedModel 是添加评论的模型。

可能的用例

  • 发送电子邮件通知

onUpdateComment

当用户编辑评论时,会触发此事件。以下属性在事件处理程序提供的第一个参数 $event 中可用

  • $event->comment 是更新评论的 ActiveRecord 实例。

onDeleteComment

当用户删除评论时,会触发此事件。以下属性在事件处理程序提供的第一个参数 $event 中可用

  • $event->comment 是被删除评论的 ActiveRecord 实例。