PHP code example of mhndev / yii2-rate

1. Go to this page and download the library: Download mhndev/yii2-rate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mhndev / yii2-rate example snippets


composer 

"mhndev/yii2-rate": "0.0.*"


return [
    'userClass' => \app\models\User::class,
    'RateClass' => \mhndev\yii2Rate\Models\Rate::class,

    'items'=>[
        'post' => [
            'class'=> \app\models\Post::class,
            'rate_types' => ['rate','like'],
            'rate_values' => [
                'class' => \mhndev\rate\DiscreteNumberValue::class,
                'values' => ['1','2','3','4','5']
            ]
        ]
    ]
];


mhndev\rate\Interfaces\iUser


    /**
     * @param $value
     * @param iRateableEntity $entity
     * @param $type
     * @return Rate
     */
    public function doRate($value, iRateableEntity $entity, $type)
    {
        $rate = new Rate;

        $rate->type = $type;
        $rate->entity = get_class($entity);
        $rate->entity_id  = $entity->_id->__toString();
        $rate->owner = static::class;
        $rate->owner_id = Yii::$app->user->identity->id;
        $rate->value = $value;

        $rate->save();

        return $rate;
    }

    $post = Post::findOne(1);
    $user = Yii::$app->user->identity;
    $user->like($post);

    $post = Post::findOne(1);
    $user = Yii::$app->user->identity;
    $user->rate(+2, $post, 'rate');