FavoriteStores.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ XI55P26SAet3EsxVaMQj0cO0a/ezcehuIZfgM6F6xsA=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.0
  11. */
  12. /**
  13. * favorite stores table service class
  14. */
  15. namespace Ppb\Service;
  16. use Cube\Db\Expr,
  17. Ppb\Db\Table\FavoriteStores as FavoriteStoresTable;
  18. class FavoriteStores extends AbstractService
  19. {
  20. /**
  21. *
  22. * class constructor
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. $this->setTable(
  28. new FavoriteStoresTable());
  29. }
  30. /**
  31. *
  32. * create or update an advert
  33. *
  34. * @param array $data
  35. * @return $this
  36. */
  37. public function save($data)
  38. {
  39. $row = null;
  40. $data = $this->_prepareSaveData($data);
  41. if (array_key_exists('id', $data)) {
  42. $select = $this->_table->select()
  43. ->where("id = ?", $data['id']);
  44. unset($data['id']);
  45. $row = $this->_table->fetchRow($select);
  46. }
  47. if (count($row) > 0) {
  48. $this->_table->update($data, "id='{$row['id']}'");
  49. }
  50. else {
  51. $data['created_at'] = new Expr('now()');
  52. $this->_table->insert($data);
  53. }
  54. return $this;
  55. }
  56. /**
  57. *
  58. * delete a favorite store row from the table
  59. *
  60. * @param int $id the id of the row to be deleted
  61. * @param int $userId the id of owner of the row
  62. * @return int returns the number of affected rows
  63. */
  64. public function delete($id, $userId = null)
  65. {
  66. $adapter = $this->_table->getAdapter();
  67. $where[] = $adapter->quoteInto('id = ?', $id);
  68. if ($userId !== null) {
  69. $where[] = $adapter->quoteInto('user_id = ?', $userId);
  70. }
  71. return $this->_table->delete($where);
  72. }
  73. }