DBObject.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. ApplicationSettings::RegisterDefaultSetting("database", "host", "localhost");
  3. ApplicationSettings::RegisterDefaultSetting("database", "database", "php-mvc");
  4. ApplicationSettings::RegisterDefaultSetting("database", "username", "root");
  5. ApplicationSettings::RegisterDefaultSetting("database", "password", "");
  6. class DBObject implements ISavableObject{
  7. protected static $PDO=null;
  8. protected static $PREPARED_STATEMENTS=array();
  9. private static $_classFields=array();
  10. protected $_fields=array();
  11. protected $_changedFields=array();
  12. private $_table,$_key,$_id;
  13. protected static function SetupPDO(){
  14. if (self::$PDO!=null)
  15. return;
  16. $host=ApplicationSettings::GetSetting("database", "host");
  17. $db=ApplicationSettings::GetSetting("database", "database");
  18. $username=ApplicationSettings::GetSetting("database", "username");
  19. $password=ApplicationSettings::GetSetting("database", "password");
  20. self::$PDO=new PDO("mysql:host=$host;dbname=$db",$username,$password);
  21. self::$PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
  22. }
  23. public static function GetPDO(){
  24. self::SetupPDO();
  25. return self::$PDO;
  26. }
  27. public static function VariableToDBField($variableName) {
  28. $parts=preg_split('/(?=[A-Z])/', $variableName);
  29. for ($i=0;$i<count($parts);$i++)
  30. $parts[$i]=strtolower($parts[$i]);
  31. 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 _
  32. }
  33. public static function DBFieldToVariable($fieldName) {
  34. $parts=explode("_",$fieldName);
  35. for ($i=0;$i<count($parts);$i++)
  36. $parts[$i]=ucfirst($parts[$i]);
  37. return implode("", $parts);
  38. }
  39. function __construct($table, $key, $id) {
  40. $this->_table=$table;
  41. $this->_key=$key;
  42. $this->_id=$id;
  43. self::SetupPDO();
  44. $this->Load();
  45. }
  46. public function __get($name) {
  47. if (array_key_exists($name,$this->_fields))
  48. return $this->_fields[$name];
  49. return null;
  50. }
  51. public function __set($name, $value) {
  52. if (array_key_exists($name,$this->_fields) && $this->_fields[$name]!=$value){
  53. $this->_changedFields[$name]=$name;
  54. $this->_fields[$name]=$value;
  55. }
  56. }
  57. public function Load(){
  58. $class=get_class($this);
  59. if (!isset(self::$_classFields[$class])){
  60. self::$_classFields[$class]=self::$PDO->query("DESCRIBE `{$this->_table}`")->fetchAll(PDO::FETCH_COLUMN);
  61. }
  62. $statementKey=$class.'_construct';
  63. if (!isset(self::$PREPARED_STATEMENTS[$statementKey])){
  64. $fields=implode(", ", self::$_classFields[$class]);
  65. $sql="SELECT $fields FROM `{$this->_table}` WHERE `{$this->_key}`=?";
  66. self::$PREPARED_STATEMENTS[$statementKey]=self::$PDO->prepare($sql);
  67. }
  68. $prep=self::$PREPARED_STATEMENTS[$statementKey];
  69. $prep->execute(array($this->_id));
  70. $record=$prep->fetch();
  71. if ($record!==false)
  72. foreach ($record as $key=>$value)
  73. $this->_fields[self::DBFieldToVariable($key)]=$value;
  74. else {
  75. foreach (self::$_classFields[$class] as $field)
  76. $this->_fields[self::DBFieldToVariable($field)]=null;
  77. $this->_id=0;
  78. }
  79. }
  80. public function Save() {
  81. if (count($this->_changedFields)==0)
  82. return;
  83. $fields=array();
  84. $execData=array();
  85. foreach ($this->_changedFields as $field){
  86. $fields[]=self::VariableToDBField($field).'=:'.$field;
  87. $execData[':'.$field]=$this->_fields[$field];
  88. }
  89. if ($this->_id!==0){
  90. $sql="UPDATE `{$this->_table}` SET ".implode(", ", $fields)." WHERE `{$this->_key}`=:soi5yh58y";
  91. $execData[':soi5yh58y']=$this->_id;
  92. }else{
  93. $sql="INSERT INTO `{$this->_table}` SET ".implode(", ", $fields);
  94. }
  95. $prep=self::$PDO->prepare($sql);
  96. $prep->execute($execData);
  97. /*$errorInfo=$prep->errorInfo();
  98. if ($errorInfo[0]!='00000')
  99. trigger_error($errorInfo[2]);*/
  100. if ($this->_id===0){ // If this is a new object we want to reload fromt he DB to make sure all fields are correct.
  101. // In order to do so we need to find the value for the key we're using
  102. $id=self::$PDO->lastInsertId();
  103. $key=self::$PDO->query("SHOW INDEX FROM `{$this->_table}` WHERE Key_name='PRIMARY'")->fetch()['Column_name'];
  104. $this->_id=self::$PDO->query("SELECT `{$this->_key}` FROM `{$this->_table}` WHERE `$key`=$id")->fetchColumn();
  105. $this->Load();
  106. }
  107. }
  108. }