PHPackages                             yaso/entity-rating-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yaso/entity-rating-bundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

yaso/entity-rating-bundle
=========================

Simplifying entities rating in symfony 4|3

2.0.2(7y ago)07MITPHPPHP &gt;=7

Since Feb 5Pushed 7y ago1 watchersCompare

[ Source](https://github.com/mohamedyasser/EntityRatingBundle)[ Packagist](https://packagist.org/packages/yaso/entity-rating-bundle)[ Docs](https://github.com/mohamedyasser/EntityRatingBundle)[ RSS](/packages/yaso-entity-rating-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (9)Versions (10)Used By (0)

Entity Rating bundle
--------------------

[](#entity-rating-bundle)

[![Rating example](https://camo.githubusercontent.com/0a8dc5e2faf26d4f1a2cbfee8caaffcd4e8d11910a8babf88e4876fbaed03344/687474703a2f2f73637265656e7368612e72652f36464664352f63704c526530595a51762e706e67)](https://camo.githubusercontent.com/0a8dc5e2faf26d4f1a2cbfee8caaffcd4e8d11910a8babf88e4876fbaed03344/687474703a2f2f73637265656e7368612e72652f36464664352f63704c526530595a51762e706e67)

#### Installation

[](#installation)

The easiest way is with [Composer](https://getcomposer.org/) package manager

```
composer require yaso/entity-rating-bundle

```

Register the bundle:

```
// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // ...
        new Yaso\Bundle\EntityRatingBundle\YasoEntityRatingBundle(),
        // ...
    );
}
```

Import routes:

```
# app/config/routing.yml
yaso_entity_rating:
    resource: "@YasoEntityRatingBundle/Resources/config/routing.yml"
    prefix:   /
```

#### Configuration

[](#configuration)

Extending the abstract entity to create your own

```
// src/Acme/AppBundle/Entity/EntityRate.php

namespace Acme\AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Yaso\Bundle\EntityRatingBundle\Entity\EntityRate as BaseEntityRate;

/**
 * EntityRate
 * @ORM\Table(name="entity_rate")
 * @ORM\Entity(repositoryClass="Yaso\Bundle\EntityRatingBundle\Repository\EntityRateRepository")
 */
class EntityRate extends BaseEntityRate
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @return mixed
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }
}
```

Add the annotation to your entity class:

```
# src/Acme/AppBundle/Entity/Post.php

use Yaso\Bundle\EntityRatingBundle\Annotation\Rated;
...

/**
 * Post
 * @ORM\Table(name="post")
 * ...
 * @Rated(min=1, max=5, step=1)
 * ...
 */
```

Configure the bundle:

```
# app/config/config.yml
yaso_entity_rating:
     # Your EntityRate namespace, persisted in the DB (according to the namespace of the entity previously created)
     entity_rating_class: Acme\AppBundle\Entity\EntityRate
     # If you decide to extend the default manager, put the service name here
     entity_rating_manager_service: acme.entity_rating.manager
     # Maximum number of rate allowed by IP for a given entity
     rate_by_ip_limitation: 10
     # Map the alias used in frontend and the corresponding class
     map_type_to_class:
       post: Acme\AppBundle\Entity\Post
       # If you need several rated entity, just add them here
       article: Acme\AppBundle\Entity\Article

# In order to print the Rating field, pass the template of the field to twig
twig:
    form_themes:
        - "YasoEntityRatingBundle:form:fields.html.twig"
```

Generate the rating form in the controller:

```
$entityRatingManager = $this->get('yaso.entity_rating_bundle.manager');
$ratingForm          = $entityRatingManager->generateForm(Post::RATING_ALIAS, $post->getId());
$globalRateData      = $entityRatingManager->getGlobalRateData($post->getId(), Post::RATING_ALIAS);

/** @var EntityRate $rate */
if ($rate = $entityRatingManager->getUserCurrentRate($post->getId(), Post::RATING_ALIAS)) {
    $ratingForm->get('rate')->setData($rate->getRate());
}

return $this->render(
    '@AcmeApp/Blog/show.html.twig',
    [
        'ratingForm'       => $ratingForm->createView(),
        'globalRateData'   => $globalRateData,
    ]
);
```

Display the form in the view:

```
{% include 'YasoEntityRatingBundle::ratingWidget.html.twig' with {'form':ratingForm, 'globalRateData':globalRateData} only %}
```

#### Importing the assets

[](#importing-the-assets)

You can import them directly:

JS

```

```

CSS

```

```

Or use a task manager (gulp/grunt...) to minify/concat/uglify them before serving them.

#### Init the JS

[](#init-the-js)

```
new EntityRating({
    form             : $('.entityrating-form'),
    radioButtonClass : '.star-rating-item',
    successCallback  : function (response) {
        var ratingWidgetSelector = $('.entity-rating-widget-wrapper[data-entity-rating-id="' + response.entityRatingId + '"]');
        ratingWidgetSelector.find('.entity-rating-rate-container').slideDown();
        ratingWidgetSelector.find('*[itemprop="ratingCount"]').text(response.rateData.rateCount);
        ratingWidgetSelector.find('*[itemprop="ratingValue"]').text(response.rateData.averageRate);
    },
    errorCallback    : function (response) {
        // Do something in case of error
    }
});
```

### Advanced usage

[](#advanced-usage)

#### Adding a relationship to the Rate entity

[](#adding-a-relationship-to-the-rate-entity)

Example: **saving the logged user in the rate Entity**

1. Add the user field to the entity

```
 /**
  * @var User
  * @ORM\ManyToOne(targetEntity="Acme\AppBundle\Entity\User")
  * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  */
 protected $user;

 /**
  * @return User
  */
 public function getUser(): User
 {
   return $this->user;
 }

 /**
  * @param User $user
  */
 public function setUser(User $user)
 {
     $this->user = $user;
 }
```

2. Extend the default manager to handle the User

```
namespace Acme\AppBundle\Manager;

use Yaso\Bundle\EntityRatingBundle\Entity\EntityRateInterface;
use Yaso\Bundle\EntityRatingBundle\Factory\EntityRatingFormFactory;
use Yaso\Bundle\EntityRatingBundle\Manager\EntityRatingManager as BaseEntityRatingManager;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\EntityManager;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class EntityRatingManager extends BaseEntityRatingManager
{

    private $user = null;

    public function __construct(
        AnnotationReader $annotationReader,
        EntityRatingFormFactory $formFactory,
        EventDispatcherInterface $eventDispatcher,
        EntityManager $entityManager,
        $entityRatingClass,
        $mapTypeToClass,
        $rateByIpLimitation,
        TokenStorage $tokenStorage
        ){
        parent::__construct($annotationReader, $formFactory, $eventDispatcher, $entityManager, $entityRatingClass, $mapTypeToClass, $rateByIpLimitation);

        /** @var TokenInterface $token */
        $token = $tokenStorage->getToken();
        if ($token !== null && is_object($token->getUser())) {
            $this->user = $token->getUser();
        }
    }

    public function getUserCurrentRate($entityId, $entityType, $ignoreFields = [])
    {
        if ($this->user) {
            return $this->entityRateRepository->findOneBy(
                [
                    'entityId'   => $entityId,
                    'entityType' => $entityType,
                    'user'       => $this->user,
                ]
            );
        } else {
            return parent::getUserCurrentRate($entityId, $entityType, ['user']);
        }
    }

    /**
     * @param \Acme\AppBundle\Entity\EntityRate|EntityRateInterface $rate
     * @param $entityId
     * @param $entityType
     * @param $rateValue
     *
     * @return \Acme\AppBundle\Entity\EntityRate|EntityRateInterface
     */
    protected function hydrateEntity(EntityRateInterface $rate, $entityId, $entityType, $rateValue)
    {
        if ($this->user) {
            $rate->setUser($this->user);
        }
        parent::hydrateEntity($rate, $entityId, $entityType, $rateValue);

        return $rate;
    }
}
```

3. Define a new manager service to use in your controller

```
acme.entity_rating.manager:
    class: Acme\AppBundle\Manager\EntityRatingManager
    parent: yaso.entity_rating_bundle.manager
    arguments: ['@security.token_storage']
```

#### Events

[](#events)

The bundle dispatches events when a rate is :

- Created : `yaso.entity_rating.rate_created`
- Updated : `yaso.entity_rating.rate_updated`

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 82% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~45 days

Recently: every ~22 days

Total

9

Last Release

2653d ago

Major Versions

1.0.5 → 2.0.12019-02-02

### Community

Maintainers

![](https://www.gravatar.com/avatar/6a2ffc4da83c5b39cc0d26eda59a7737db12d6df8523f10024a208249a843066?d=identicon)[mohamedyasser](/maintainers/mohamedyasser)

---

Top Contributors

[![cymo-tgillet](https://avatars.githubusercontent.com/u/2579903?v=4)](https://github.com/cymo-tgillet "cymo-tgillet (50 commits)")[![mohamedyasser](https://avatars.githubusercontent.com/u/16548777?v=4)](https://github.com/mohamedyasser "mohamedyasser (11 commits)")

---

Tags

symfonyreviewRatingentity ratingentity review

### Embed Badge

![Health badge](/badges/yaso-entity-rating-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/yaso-entity-rating-bundle/health.svg)](https://phpackages.com/packages/yaso-entity-rating-bundle)
```

###  Alternatives

[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M647](/packages/sylius-sylius)[easycorp/easyadmin-bundle

Admin generator for Symfony applications

4.3k16.7M308](/packages/easycorp-easyadmin-bundle)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M151](/packages/sulu-sulu)[ec-cube/ec-cube

EC-CUBE EC open platform.

78527.0k1](/packages/ec-cube-ec-cube)[contao/core-bundle

Contao Open Source CMS

1231.6M2.3k](/packages/contao-core-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
