BaseDao.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. Lib::loadClass('BaseModel');
  3. class BaseDao {
  4. var $_db;
  5. var $_table;
  6. var $_save_hist_id;
  7. var $_save_id;
  8. function __construct($db, $table) {
  9. $this->_db = $db;
  10. $this->_table = $table;
  11. $this->_save_hist_id = 0;
  12. $this->_save_id = 0;
  13. }
  14. function get_by_id( $id ) {
  15. $null = null;
  16. $id = (int)$id;
  17. if ($id <= 0) return $null;
  18. $sql = "select t.*
  19. from `" . $this->_table . "` as t
  20. where t.`ID`='" . $id . "'
  21. ";
  22. $res = DB::query($sql);
  23. if ($h = DB::fetch_assoc($res)) {
  24. $model = new BaseModel($this);
  25. $model->setDataFromDB($h);
  26. return $model;
  27. }
  28. return $null;
  29. }
  30. /**
  31. * Save $model into DB.
  32. * @returns boolean or integer - created record ID.
  33. */
  34. function save($model) {
  35. $ret = false;
  36. $this->_save_hist_id = 0;
  37. $this->_save_id = 0;
  38. if ($model->get('ID') > 0) {
  39. $this->_save_id = $model->get('ID');
  40. $sql_data = array();
  41. $data = $model->changes_to_array();
  42. if (!empty($data)) {
  43. $data['A_RECORD_UPDATE_DATE'] = date('Y-m-d-H:i');
  44. $data['A_RECORD_UPDATE_AUTHOR'] = User::getName();
  45. foreach ($data as $k_field => $v_value) {
  46. $sql_data []= "`" . $k_field . "`='" . $v_value . "'";
  47. }
  48. $sql = "update `" . $this->_table . "` set " . implode(", ", $sql_data) . " where `ID`='" . $model->get('ID') . "'; ";
  49. $this->_db->query($sql);
  50. if ($this->_db->affected_rows() > 0) {
  51. $ret = true;
  52. }
  53. } else {
  54. $ret = true;
  55. }
  56. }
  57. else {
  58. $sql_data = array();
  59. $data = $model->to_array();
  60. $data['A_RECORD_CREATE_DATE'] = date('Y-m-d-H:i');
  61. $data['A_RECORD_CREATE_AUTHOR'] = User::getName();
  62. foreach ($data as $k_field => $v_value) {
  63. if (!empty($v_value)) {
  64. $sql_data["`" . $k_field . "`"] = "'" . $v_value . "'";
  65. }
  66. }
  67. $sql = "insert into `" . $this->_table . "` (" . implode(", ", array_keys($sql_data)) . ") values (" . implode(", ", array_values($sql_data)) . "); ";
  68. $this->_db->query($sql);
  69. $this->_save_id = $this->_db->insert_id();
  70. if ($this->_save_id > 0) {
  71. $ret = $this->_save_id;
  72. }
  73. }
  74. {// update _HIST if anything changed/created
  75. if ($this->_save_id > 0 && !empty($data)) {
  76. $sql_data = array();
  77. foreach ($data as $key => $val) {
  78. $sql_data["`" . $key . "`"] = "'" . $val . "'";
  79. }
  80. $sql_data["`ID_USERS2`"] = "'" . $this->_save_id . "'";
  81. $sql_hist = "insert into `" . $this->_table . "_HIST` (" . implode(", ", array_keys($sql_data)) . ") values (" . implode(", ", array_values($sql_data)) . "); ";
  82. $this->_db->query($sql_hist);
  83. $this->_save_hist_id = $this->_db->insert_id();
  84. }
  85. }
  86. return $ret;
  87. }
  88. public function get_saved_id() {
  89. return $this->_save_id;
  90. }
  91. public function get_saved_hist_id() {
  92. return $this->_save_hist_id;
  93. }
  94. }