tadcka / object-rating-bundle
Symfony2上的对象评分
v1.0.0
2013-11-23 14:02 UTC
Requires
- php: >=5.3.2
- doctrine/orm: >=2.1.0
- symfony/symfony: >=2.2
This package is not auto-updated.
Last update: 2024-09-24 01:58:25 UTC
README
安装
步骤 1: 使用composer下载TadckaAddressBundle
在composer.json中添加TadckaAddressBundle
{ "require": { "tadcka/object-rating-bundle": "dev-master" } }
现在运行以下命令让composer下载bundle
$ php composer.phar update tadcka/object-rating-bundle
步骤 2: 启用bundle
在kernel中启用bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Tadcka\ObjectRatingBundle\TadckaObjectRatingBundle(), ); }
步骤 3: 更新doctrine模式
$ php app/console doctrine:schema:update --dump-sql
步骤 4: 包含javascript和css
@TadckaObjectRatingBundle/Resources/public/css/jquery.rating.css @TadckaObjectRatingBundle/Resources/public/js/star-rating/jquery.rating.js @TadckaObjectRatingBundle/Resources/public/js/object-rating.js
步骤 5: 创建对象评分信息
构建对象评分信息并将其包含到模板中
/** * @return \Tadcka\ObjectRatingBundle\Manager\ObjectRatingManager */ private function getObjectRatingManager() { return $this->get('tadcka_object_rating.manager'); } public function exampleAction() { $example = new \Tadcka\ExampleBundle\Entity\Example(); return $this->render('TadckaExampleBundle:Example:example.html.twig', array( 'object_rating_info' => $this->getObjectRatingManager() ->getObjectRatingInfo(Example::OBJECT_TYPE, $example->getId()), )); }
或列表
public function examplesAction() { $ids = array(1, 2); return $this->render('TadckaExampleBundle:Example:examples.html.twig', array( 'objects_rating_info' => $this->getObjectRatingManager() ->getObjectsRatingInfo(Example::OBJECT_TYPE, $ids);, )); }
步骤 6: 在twig模板中渲染对象评分
{% include 'TadckaObjectRatingBundle::show_object_rating.html.twig' with { url: url("exmaple"), objectRatingInfo: object_rating_info } %}
或列表
{% if objects_rating_info[example.getId()] is defined %} {% include 'TadckaObjectRatingBundle::show_object_rating.html.twig' with { url: url("exmaple"), objectRatingInfo: objects_rating_info[example.getId()] } %} {% endif %}
步骤 7: twig模板中的对象评分表单
{% render url('tadcka_object_rating', {objectType: 'example', objectId: example.getId() }) %}
步骤 8: 添加对象评分事件监听器
<?php namespace Tadcka\ExampleBundle\EventListener; use Tadcka\ObjectRatingBundle\Event\ObjectRatingEvent; use Tadcka\ObjectRatingBundle\Services\ObjectRatingService; class ObjectRatingListener { /** * @var ObjectRatingService */ private $objectRatingService; public function setObjectRatingService(ObjectRatingService $objectRatingService) { $this->objectRatingService = $objectRatingService; } public function onExampleRating(ObjectRatingEvent $event) { $objectRating = $event->getObjectRating(); $this->objectRatingService->subscribe($objectRating); } }
事件名称为前缀 "tadcka_object_rating.event." 和对象类型 "example"
<parameter key="tadcka_example.rating_event_listener.class" >Tadcka\ExampleBundle\EventListener\ObjectRatingListener</parameter> <service id="tadcka_example.rating_event_listener" class="%tadcka_example.rating_event_listener.class%"> <call method="setObjectRatingService"> <argument type="service" id="tadcka_object_rating" /> </call> <tag name="kernel.event_listener" event="tadcka_object_rating.event.example" method="onExampleRating"/> </service>