DBObject.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. class DBObject implements ISavableObject, JsonSerializable {
  3. protected static $PREPARED_STATEMENTS=array();
  4. private static $_classFields=array();
  5. protected $_fields=array();
  6. protected $_changedFields=array();
  7. private $_table, $_keys, $_ids;
  8. public static function VariableToDBField($variableName) {
  9. $parts=preg_split('/(?=[A-Z])/', $variableName);
  10. for ($i=0; $i<count($parts); $i++)
  11. $parts[$i]=strtolower($parts[$i]);
  12. return trim(implode("_", $parts), "_"); // If the variable name start with upper case then we get an extra blank entry in the array causing an extra _
  13. }
  14. public static function DBFieldToVariable($fieldName) {
  15. $parts=explode("_", $fieldName);
  16. for ($i=0; $i<count($parts); $i++)
  17. $parts[$i]=ucfirst($parts[$i]);
  18. return implode("", $parts);
  19. }
  20. private function ReIndexKeyAndIdArrays() {
  21. $newKeyArray=array();
  22. foreach ($this->_keys as $index=> $key) {
  23. $newKeyArray[':'.$index]=$key;
  24. }
  25. $this->_keys=$newKeyArray;
  26. $newIdArray=array();
  27. foreach ($this->_ids as $index=> $id) {
  28. $newIdArray[':'.$index]=$id;
  29. }
  30. $this->_ids=$newIdArray;
  31. }
  32. private function GetWhereClause() {
  33. $qualifiers=array();
  34. foreach ($this->_keys as $index=> $key)
  35. $qualifiers[]=" `$key`=$index";
  36. return implode(" AND", $qualifiers);
  37. }
  38. function __construct($table, $key, $id) {
  39. $this->_table=$table;
  40. $this->_keys=array();
  41. $this->_ids=array();
  42. if (is_array($key)&&is_array($id)) {
  43. $this->_keys=$key;
  44. $this->_ids=$id;
  45. } elseif ($id!=null && ($id!="" || (int)$id!=0)) {
  46. $this->_keys=array($key);
  47. $this->_ids=array($id);
  48. }
  49. $this->ReIndexKeyAndIdArrays();
  50. $this->Load();
  51. }
  52. public function __get($name) {
  53. if (array_key_exists($name, $this->_fields))
  54. return $this->_fields[$name];
  55. return null;
  56. }
  57. public function __set($name, $value) {
  58. if (array_key_exists($name, $this->_fields)&&$this->_fields[$name]!==$value) {
  59. $this->_changedFields[$name]=$name;
  60. $this->_fields[$name]=$value;
  61. }
  62. }
  63. public function __isset($name) {
  64. return array_key_exists($name, $this->_fields);
  65. }
  66. public function Load() {
  67. $PDO=BaseRepository::GetPDO();
  68. $class=get_class($this);
  69. if (!isset(self::$_classFields[$class])) {
  70. self::$_classFields[$class]=$PDO->query("DESCRIBE `{$this->_table}`")->fetchAll(PDO::FETCH_COLUMN);
  71. }
  72. foreach (self::$_classFields[$class] as $field)
  73. $this->_fields[self::DBFieldToVariable($field)]=null;
  74. if (count($this->_keys)>0){
  75. $statementKey=$class.'_construct_'.$this->_table.implode('', $this->_keys);
  76. if (!isset(self::$PREPARED_STATEMENTS[$statementKey])) {
  77. $fields=implode("`, `", self::$_classFields[$class]);
  78. $sql="SELECT `$fields` FROM `{$this->_table}` WHERE".$this->GetWhereClause();
  79. self::$PREPARED_STATEMENTS[$statementKey]=$PDO->prepare($sql);
  80. }
  81. $prep=self::$PREPARED_STATEMENTS[$statementKey];
  82. $prep->execute($this->_ids);
  83. $errorInfo=$prep->errorInfo();
  84. if ($errorInfo[0]!='00000'){
  85. Utils::var_dump_email($prep);
  86. throw new Exception($errorInfo[2]);
  87. }
  88. $record=$prep->fetch();
  89. if ($record!==false)
  90. foreach ($record as $key=> $value)
  91. $this->_fields[self::DBFieldToVariable($key)]=$value;
  92. else {
  93. $this->_keys=array();
  94. $this->_ids=array();
  95. }
  96. }
  97. }
  98. protected function HasChangedFields() {
  99. return count($this->_changedFields)>0;
  100. }
  101. public function Save() {
  102. $PDO=BaseRepository::GetPDO();
  103. if (!$this->HasChangedFields())
  104. return;
  105. $fields=array();
  106. $execData=array();
  107. foreach ($this->_changedFields as $field) {
  108. $fields[]='`'.self::VariableToDBField($field).'`=:'.$field;
  109. $execData[':'.$field]=$this->_fields[$field];
  110. }
  111. if (count($this->_ids)!==0) {
  112. $sql="UPDATE `{$this->_table}` SET ".implode(", ", $fields)." WHERE".$this->GetWhereClause();
  113. $execData=array_merge($execData, $this->_ids);
  114. } else {
  115. $sql="INSERT INTO `{$this->_table}` SET ".implode(", ", $fields);
  116. }
  117. $prep=$PDO->prepare($sql);
  118. $prep->execute($execData);
  119. $errorInfo=$prep->errorInfo();
  120. if ($errorInfo[0]!='00000')
  121. throw new Exception($errorInfo[2]);
  122. if (count($this->_ids)===0) { // If this is a new object we want to reload fromt he DB to make sure all fields are correct.
  123. // In order to do so we need to find the value for the key we're using
  124. $id=$PDO->lastInsertId();
  125. if ($id!=0 && count($this->_keys)==0) {
  126. $this->_keys=$this->_ids=array();
  127. $key=$PDO->query("SHOW INDEX FROM `{$this->_table}` WHERE Key_name='PRIMARY'")->fetch()['Column_name'];
  128. $this->_keys[]=$key;
  129. $this->_ids[]=$PDO->query("SELECT `$key` FROM `{$this->_table}` WHERE `$key`=$id")->fetchColumn();
  130. $this->ReIndexKeyAndIdArrays();
  131. }
  132. $this->Load();
  133. }
  134. }
  135. public function jsonSerialize() {
  136. $obj=new stdClass();
  137. foreach ($this->_fields as $key=> $value) {
  138. $obj->$key=$value;
  139. }
  140. return $obj;
  141. }
  142. }