waltertamboer/wtrating

该软件包最新版本(dev-master)没有提供许可证信息。

一个提供评分或点赞功能的Zend Framework 2模块。

dev-master 2012-11-04 20:05 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:24:07 UTC


README

版本 0.0.1

Build Status

简介

这是一个提供评分或点赞功能的Zend Framework 2模块。

要求

安装

将此仓库作为子模块添加到您的仓库,或者使用Composer进行安装

{
    "require": {
        "waltertamboer/wtrating": "*"
    }
}

使用方法

设置

设置您想要使用的mapper。默认情况下,此模块仅包含一个Zend\Db mapper。

'service_manager' => array(
    'factories' => array(
        'wtrating.mapper' => function ($sm) {
            $dbAdapter = $sm->get('... db adapter ...');
            return new \WtRating\Mapper\ZendDbMapper($dbAdapter);
        }
    ),
)

评分

要添加评分,您可以执行以下操作

public function indexAction()
{
    // The id of the user that is currently logged in or null if there is no user:
    $userId = ...;

    // The type that identifies the rating:
    $typeId = 'my-article-163';

    // The rating to set, make something up:
    $rating = rand();

    $serviceLocator = $this->getServiceLocator();

    // Create a new rating:
    $rating = $serviceLocator->create('wtrating.rating');
    $rating->setTypeId($typeId);
    $rating->setUserId($userId);
    $rating->setRating($rating);

    // Save the rating to the storage device:
    $ratingService = $serviceLocator->get('wtrating.service');
    $ratingService->persist($rating);

    // Retrieve the rating set that contains information like avarage rating,
    // amount of rates, etc.
    return new ViewModel(array(
        'ratingSet' => $ratingService->getRatingSet($typeId)
    ));
}

显示评分

存在一个视图助手可以调用

$this->wtRating($ratingSet);

还可以添加属性

$this->wtRating($this->ratingSet, array(
    'class' => 'rating'
));

如果您想更改HTML元素,可以使用第三个参数

$this->wtRating($this->ratingSet, array(), 'div');