音乐独奏 / 计数器包
Symfony 的计数器包
v1.1
2016-07-09 14:11 UTC
Requires
- php: >=5.4.0
- doctrine/orm: ^2.3
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-09-14 18:58:45 UTC
README
Symfony 包可以帮助您轻松地为实体添加计数器。在实体中使用 CounterTrait
将自动创建计数器实体关系。然后您可以轻松地添加点击到计数器中。您还可以通过保留实际计数来为实体添加假计数。
安装
1-) 运行以下命令告诉 composer 下载:
composer require pianosolo/counter-bundle
2-) 将包添加到 AppKernel
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new PianoSolo\CounterBundle\PianoSoloCounterBundle(), ); }
使用方法
1-) 将 CounterTrait
添加到您的实体中。
<?php namespace MyBundle\Entity; use PianoSolo\Traits\CounterTrait; class MyEntity { use CounterTrait; //... }
2-) 事件监听器将在持久化您的实体时为您创建一个新的 Counter
。
$myEntity = new MyEntity(); $entityManager->persist($myEntity); $entityManager->flush();
3-) 调用 Counter 并添加点击。
<?php namespace MyBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class DefaultController extends Controller { public function showEntityAction() { //... $myEntity = $myEntityRepository->findOneBy(array('id' => $id)); $myEntity->getCounter()->addClick(5); // Default value of parameter is 1 $entityManager->persist($myEntity); $entityManager->flush(); } }
4-) 获取计数
$count = $myEntity->getCounter()->getCount();
{{ myEntity.counter.count }}
添加假计数
您可以为实体添加假计数并保留实际计数。当您想删除这些假计数时,可以随时进行删除。
// Example initial count values of entity $count = $myEntity->getCounter()->getCount(); // return 10 $realCount = $myEntity->getCounter()->getRealCount(); // return 10 // Adding fake count $myEntity->getCounter()->setCorrectionCount(100); $entityManager->persist($myEntity); $entityManager->flush(); // Keeping real count $count = $myEntity->getCounter()->getCount(); // return 110 $realCount = $myEntity->getCounter()->getRealCount(); // return 10 // Delete fake count $myEntity->getCounter()->setCorrectionCount(0): $entityManager->persist($myEntity); $entityManager->flush();