![]() |
|
sfPropelActAsRatableBehaviorPlugin - 0.6.1Propel ratable behavior |
|
This plugin aims at providing rating capabilities to any Propel object with the help of a dedicated Propel behavior.

To install the plugin, run this command within your symfony project :
symfony plugin-install http://plugins.symfony-project.com/sfPropelActAsRatableBehaviorPlugin
To activate this Propel behavior in Symfony, you must first activate behaviors in your propel.ini file :
propel.builder.addBehaviors = true
In one (or more) of your existing model object classes, apply the behavior. Eg.
for an Article Propel model class:
<?php
class Article extends BaseArticle
{
}
sfPropelBehavior::add('Article', array('sfPropelActAsRatableBehavior'));
You can add this optionals parameters when applying the bahavior:
max_rating: sets the maximum rating available for an object (this must
be an integer - default is 5)reference_field: sets the name of the field which will reference the
object (by default it is automatically set to its primary key field, so
leave this if you're unsure). A reference field can be a column name, a field
name or a Propel phpName.Example of use of these parameters:
<?php
class Article extends BaseArticle
{
}
sfPropelBehavior::add('Article',
array('sfPropelActAsRatableBehavior',
array('max_rating' => 10,
'reference_field' => 'StrippedTitle')));
Then, rebuild your model :
symfony propel-build-all
And clear the cache :
symfony cc
This plugin provides an Ajax-based rating system, with pretty stars to click on.

To activate this feature, you must enable the sfRating module in the
config/settings.yml file of the app you want to use the helper in :
all:
.settings:
enabled_modules: [sfRating](default,)
If you are under Microsoft Windows, you also have to manually copy the ./web
directory of the plugin in the %SF_ROOT_DIR%/web directory of your project
and rename it to sfPropelActAsRatableBehaviorPlugin. Then you will have
this on the filesytem :
project_root
[...]
web
sfPropelActAsRatableBehaviorPlugin
css
sf_rating.css
images
alt_star.gif
Then, you can use the sf_rater helper in any of your template:
<?php use_helper('sfRating') ?>
<?php echo sf_rater($article, md5($user->getId()) ?>
... where $article is of course an instance of your Article propel
class and $user->getId() is an optional unique reference to the user
currently rating your object.
The unique user reference can be an IP address:
<?php use_helper('sfRating') ?>
<?php echo sf_rater($article, md5($_SERVER['REMOTE_ADDR']) ?>
You can rate your previously configured Article objects through the
new API which as been dynamically added to your object by the behavior.
Note that you can provide a user PK to determine if one of your user has already
rated the object. Here we imagine a User object instance $user which represent a
member, an author, a person or anything like this.
To set a rating for a given user :
$article->setRating(10, $user->getId());
To retrieve user rating for this object :
$article->getUserRating($user->getId());
To get the average rating of the object :
$article->getRating();
To retrieve the maximum possible rating for an object (which you have defined in the MAX_RATING class constant) :
$article->getMaxRating();
To clear user rating :
$article->clearRating($user->getId());
To clear all ratings for the object :
$article->clearRatings();
You can test if the object has already been rated :
$article->hasBeenRated();
You can also test if the object has already been rated by a particular user:
$article->hasBeenRatedByUser($user->getId());
The plugin is provided with a test suite located in the ./test directory.
To run the tests, type this line from the root of your project :
$ php plugins/sfPropelActAsRatableBehaviorPlugin/test/sfPropelActAsRatableBehaviorTest.php
Note that you have to provide a Propel test object class name to run the test in the test file:
define('TEST_CLASS', 'Article');
sfRatings table has been renamed to sf_ratings: you have to
rebuild your SQL files and insert them in your DB if you upgrade from
0.5.0. Hopefully, one day we'll have a migration system in Symfony core...