DBObject.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. class DBObject implements ISavableObject {
  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. if (is_array($key)&&is_array($id)) {
  41. $this->_keys=$key;
  42. $this->_ids=$id;
  43. } else {
  44. $this->_keys=array($key);
  45. $this->_ids=array($id);
  46. }
  47. $this->ReIndexKeyAndIdArrays();
  48. $this->Load();
  49. }
  50. public function __get($name) {
  51. if (array_key_exists($name, $this->_fields))
  52. return $this->_fields[$name];
  53. return null;
  54. }
  55. public function __set($name, $value) {
  56. if (array_key_exists($name, $this->_fields)&&$this->_fields[$name]!=$value) {
  57. $this->_changedFields[$name]=$name;
  58. $this->_fields[$name]=$value;
  59. }
  60. }
  61. public function __isset($name) {
  62. return array_key_exists($name, $this->_fields);
  63. }
  64. public function Load() {
  65. $PDO=BaseRepository::GetPDO();
  66. $class=get_class($this);
  67. if (!isset(self::$_classFields[$class])) {
  68. self::$_classFields[$class]=$PDO->query("DESCRIBE `{$this->_table}`")->fetchAll(PDO::FETCH_COLUMN);
  69. }
  70. $statementKey=$class.'_construct_'.$this->_table;
  71. if (!isset(self::$PREPARED_STATEMENTS[$statementKey])) {
  72. $fields=implode("`, `", self::$_classFields[$class]);
  73. $sql="SELECT `$fields` FROM `{$this->_table}` WHERE".$this->GetWhereClause();
  74. //var_dump($sql, $this->_ids);
  75. self::$PREPARED_STATEMENTS[$statementKey]=$PDO->prepare($sql);
  76. }
  77. $prep=self::$PREPARED_STATEMENTS[$statementKey];
  78. $prep->execute($this->_ids);
  79. $errorInfo=$prep->errorInfo();
  80. if ($errorInfo[0]!='00000')
  81. throw new Exception($errorInfo[2]);
  82. $record=$prep->fetch();
  83. if ($record!==false)
  84. foreach ($record as $key=> $value)
  85. $this->_fields[self::DBFieldToVariable($key)]=$value;
  86. else {
  87. foreach (self::$_classFields[$class] as $field)
  88. $this->_fields[self::DBFieldToVariable($field)]=null;
  89. $this->_ids=0;
  90. }
  91. }
  92. public function Save() {
  93. $PDO=BaseRepository::GetPDO();
  94. if (count($this->_changedFields)==0)
  95. return;
  96. $fields=array();
  97. $execData=array();
  98. foreach ($this->_changedFields as $field) {
  99. $fields[]='`'.self::VariableToDBField($field).'`=:'.$field;
  100. $execData[':'.$field]=$this->_fields[$field];
  101. }
  102. if ($this->_ids!==0) {
  103. #$sql="UPDATE `{$this->_table}` SET ".implode(", ", $fields)." WHERE `{$this->_keys}`=:soi5yh58y";
  104. #$execData[':soi5yh58y']=$this->_ids;
  105. $sql="UPDATE `{$this->_table}` SET ".implode(", ", $fields)." WHERE".$this->GetWhereClause();
  106. $execData=array_merge($execData,$this->_ids);
  107. } else {
  108. $sql="INSERT INTO `{$this->_table}` SET ".implode(", ", $fields);
  109. }
  110. $prep=$PDO->prepare($sql);
  111. $prep->execute($execData);
  112. $errorInfo=$prep->errorInfo();
  113. if ($errorInfo[0]!='00000')
  114. throw new Exception($errorInfo[2]);
  115. 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.
  116. // In order to do so we need to find the value for the key we're using
  117. $id=$PDO->lastInsertId();
  118. if ($id!=0 && count($this->_keys)==0){
  119. $this->_keys=$this->_ids=array();
  120. $key=$PDO->query("SHOW INDEX FROM `{$this->_table}` WHERE Key_name='PRIMARY'")->fetch()['Column_name'];
  121. $this->_keys[]=$key;
  122. $this->_ids[]=$PDO->query("SELECT `$key` FROM `{$this->_table}` WHERE `$key`=$id")->fetchColumn();
  123. $this->ReIndexKeyAndIdArrays();
  124. }
  125. $this->Load();
  126. }
  127. }
  128. }