ArticleLikeCounter.class.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Cas\Dao;
  3. use KIF\Dao\AbstractDao;
  4. use KIF\Verify;
  5. use KIF\Dao\SqlHelper;
  6. class ArticleLikeCounter extends AbstractDao {
  7. protected $tableName = 'article_like_counter';
  8. protected $primaryKey = 'event_id';
  9. public function getLikes($event_id) {
  10. if (!Verify::unsignedInt($event_id)) {
  11. return 0;
  12. }
  13. $result = $this->fetchOne(array('event_id' => $event_id), 'likes');
  14. return (int) $result['likes'];
  15. }
  16. public function incrLikes($event_id) {
  17. if (!Verify::unsignedInt($event_id)) {
  18. return false;
  19. }
  20. $tableInfo = array(
  21. 'event_id' => $event_id,
  22. 'likes' => 1,
  23. );
  24. $onDuplicate = array(
  25. 'likes' => SqlHelper::wrapperNoQuote('`likes`+1'),
  26. );
  27. $tmpResult = $this->insertDuplicate($tableInfo, $onDuplicate);
  28. if (!$tmpResult) {
  29. return false;
  30. }
  31. return true;
  32. }
  33. public function decrLikes($event_id) {
  34. if (!Verify::unsignedInt($event_id)) {
  35. return false;
  36. }
  37. $updateResult = $this->update(array(
  38. 'likes' => SqlHelper::wrapperNoQuote('`likes` - 1'),
  39. ), array(
  40. 'event_id' => $event_id,
  41. ));
  42. if (!$updateResult) {
  43. return false;
  44. }
  45. return true;
  46. }
  47. }