Transactions.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ pEHl4GG201MmEykQg4FthisdSYlav1qn1D1+VLZTk9M=
  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. * transactions table service class
  14. */
  15. namespace Ppb\Service;
  16. use Ppb\Db\Table\Transactions as TransactionsTable,
  17. Cube\Db\Expr;
  18. class Transactions extends AbstractService
  19. {
  20. /**
  21. *
  22. * class constructor
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. $this->setTable(
  28. new TransactionsTable());
  29. }
  30. /**
  31. *
  32. * create or update a transaction
  33. *
  34. * @param array $data
  35. * @return int the id of the inserted/updated row
  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. $data['updated_at'] = new Expr('now()');
  49. $this->_table->update($data, "id='{$row['id']}'");
  50. $id = $row['id'];
  51. }
  52. else {
  53. $data['created_at'] = new Expr('now()');
  54. $this->_table->insert($data);
  55. $id = $this->_table->lastInsertId();
  56. }
  57. return $id;
  58. }
  59. }