|
@@ -1,115 +1,154 @@
|
|
|
<?php
|
|
|
-class DBObject implements ISavableObject{
|
|
|
+
|
|
|
+class DBObject implements ISavableObject {
|
|
|
+
|
|
|
protected static $PREPARED_STATEMENTS=array();
|
|
|
-
|
|
|
private static $_classFields=array();
|
|
|
-
|
|
|
protected $_fields=array();
|
|
|
protected $_changedFields=array();
|
|
|
-
|
|
|
- private $_table,$_key,$_id;
|
|
|
-
|
|
|
+ private $_table, $_keys, $_ids;
|
|
|
+
|
|
|
public static function VariableToDBField($variableName) {
|
|
|
$parts=preg_split('/(?=[A-Z])/', $variableName);
|
|
|
- for ($i=0;$i<count($parts);$i++)
|
|
|
+ for ($i=0; $i<count($parts); $i++)
|
|
|
$parts[$i]=strtolower($parts[$i]);
|
|
|
- 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 _
|
|
|
+ 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 _
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public static function DBFieldToVariable($fieldName) {
|
|
|
- $parts=explode("_",$fieldName);
|
|
|
- for ($i=0;$i<count($parts);$i++)
|
|
|
+ $parts=explode("_", $fieldName);
|
|
|
+ for ($i=0; $i<count($parts); $i++)
|
|
|
$parts[$i]=ucfirst($parts[$i]);
|
|
|
return implode("", $parts);
|
|
|
}
|
|
|
+
|
|
|
+ private function ReIndexKeyAndIdArrays(){
|
|
|
+ $newKeyArray=array();
|
|
|
+ foreach ($this->_keys as $index=>$key){
|
|
|
+ $newKeyArray[':'.$index]=$key;
|
|
|
+ }
|
|
|
+ $this->_keys=$newKeyArray;
|
|
|
+
|
|
|
+ $newIdArray=array();
|
|
|
+ foreach ($this->_ids as $index=>$id){
|
|
|
+ $newIdArray[':'.$index]=$id;
|
|
|
+ }
|
|
|
+ $this->_ids=$newIdArray;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function GetWhereClause(){
|
|
|
+ $qualifiers=array();
|
|
|
+ foreach ($this->_keys as $index=>$key)
|
|
|
+ $qualifiers[]=" `$key`=$index";
|
|
|
+ return implode(" AND", $qualifiers);
|
|
|
+ }
|
|
|
|
|
|
function __construct($table, $key, $id) {
|
|
|
$this->_table=$table;
|
|
|
- $this->_key=$key;
|
|
|
- $this->_id=$id;
|
|
|
-
|
|
|
+
|
|
|
+ if (is_array($key)&&is_array($id)) {
|
|
|
+ $this->_keys=$key;
|
|
|
+ $this->_ids=$id;
|
|
|
+ } else {
|
|
|
+ $this->_keys=array($key);
|
|
|
+ $this->_ids=array($id);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->ReIndexKeyAndIdArrays();
|
|
|
$this->Load();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public function __get($name) {
|
|
|
- if (array_key_exists($name,$this->_fields))
|
|
|
+ if (array_key_exists($name, $this->_fields))
|
|
|
return $this->_fields[$name];
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public function __set($name, $value) {
|
|
|
- if (array_key_exists($name,$this->_fields) && $this->_fields[$name]!=$value){
|
|
|
+ if (array_key_exists($name, $this->_fields)&&$this->_fields[$name]!=$value) {
|
|
|
$this->_changedFields[$name]=$name;
|
|
|
$this->_fields[$name]=$value;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public function __isset($name) {
|
|
|
return array_key_exists($name, $this->_fields);
|
|
|
}
|
|
|
-
|
|
|
- public function Load(){
|
|
|
+
|
|
|
+ public function Load() {
|
|
|
$PDO=BaseRepository::GetPDO();
|
|
|
-
|
|
|
+
|
|
|
$class=get_class($this);
|
|
|
-
|
|
|
- if (!isset(self::$_classFields[$class])){
|
|
|
+
|
|
|
+ if (!isset(self::$_classFields[$class])) {
|
|
|
self::$_classFields[$class]=$PDO->query("DESCRIBE `{$this->_table}`")->fetchAll(PDO::FETCH_COLUMN);
|
|
|
}
|
|
|
-
|
|
|
- $statementKey=$class.'_construct_'.$this->_key;
|
|
|
- if (!isset(self::$PREPARED_STATEMENTS[$statementKey])){
|
|
|
- $fields=implode(", ", self::$_classFields[$class]);
|
|
|
- $sql="SELECT $fields FROM `{$this->_table}` WHERE `{$this->_key}`=?";
|
|
|
+
|
|
|
+ $statementKey=$class.'_construct_'.$this->_table;
|
|
|
+ if (!isset(self::$PREPARED_STATEMENTS[$statementKey])) {
|
|
|
+ $fields=implode("`, `", self::$_classFields[$class]);
|
|
|
+ $sql="SELECT `$fields` FROM `{$this->_table}` WHERE".$this->GetWhereClause();
|
|
|
+ //var_dump($sql, $this->_ids);
|
|
|
self::$PREPARED_STATEMENTS[$statementKey]=$PDO->prepare($sql);
|
|
|
}
|
|
|
$prep=self::$PREPARED_STATEMENTS[$statementKey];
|
|
|
- $prep->execute(array($this->_id));
|
|
|
+ $prep->execute($this->_ids);
|
|
|
+
|
|
|
+ $errorInfo=$prep->errorInfo();
|
|
|
+ if ($errorInfo[0]!='00000')
|
|
|
+ throw new Exception($errorInfo[2]);
|
|
|
+
|
|
|
$record=$prep->fetch();
|
|
|
if ($record!==false)
|
|
|
- foreach ($record as $key=>$value)
|
|
|
+ foreach ($record as $key=> $value)
|
|
|
$this->_fields[self::DBFieldToVariable($key)]=$value;
|
|
|
else {
|
|
|
foreach (self::$_classFields[$class] as $field)
|
|
|
$this->_fields[self::DBFieldToVariable($field)]=null;
|
|
|
- $this->_id=0;
|
|
|
+ $this->_ids=0;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public function Save() {
|
|
|
$PDO=BaseRepository::GetPDO();
|
|
|
-
|
|
|
+
|
|
|
if (count($this->_changedFields)==0)
|
|
|
return;
|
|
|
-
|
|
|
+
|
|
|
$fields=array();
|
|
|
$execData=array();
|
|
|
- foreach ($this->_changedFields as $field){
|
|
|
- $fields[]=self::VariableToDBField($field).'=:'.$field;
|
|
|
+ foreach ($this->_changedFields as $field) {
|
|
|
+ $fields[]='`'.self::VariableToDBField($field).'`=:'.$field;
|
|
|
$execData[':'.$field]=$this->_fields[$field];
|
|
|
}
|
|
|
-
|
|
|
- if ($this->_id!==0){
|
|
|
- $sql="UPDATE `{$this->_table}` SET ".implode(", ", $fields)." WHERE `{$this->_key}`=:soi5yh58y";
|
|
|
- $execData[':soi5yh58y']=$this->_id;
|
|
|
- }else{
|
|
|
+
|
|
|
+ if ($this->_ids!==0) {
|
|
|
+ #$sql="UPDATE `{$this->_table}` SET ".implode(", ", $fields)." WHERE `{$this->_keys}`=:soi5yh58y";
|
|
|
+ #$execData[':soi5yh58y']=$this->_ids;
|
|
|
+ $sql="UPDATE `{$this->_table}` SET ".implode(", ", $fields)." WHERE".$this->GetWhereClause();
|
|
|
+ $execData=array_merge($execData,$this->_ids);
|
|
|
+ } else {
|
|
|
$sql="INSERT INTO `{$this->_table}` SET ".implode(", ", $fields);
|
|
|
}
|
|
|
-
|
|
|
$prep=$PDO->prepare($sql);
|
|
|
$prep->execute($execData);
|
|
|
-
|
|
|
- //var_dump($prep->errorInfo());
|
|
|
- /*$errorInfo=$prep->errorInfo();
|
|
|
+
|
|
|
+ $errorInfo=$prep->errorInfo();
|
|
|
if ($errorInfo[0]!='00000')
|
|
|
- trigger_error($errorInfo[2]);*/
|
|
|
-
|
|
|
- if ($this->_id===0){ // If this is a new object we want to reload fromt he DB to make sure all fields are correct.
|
|
|
+ throw new Exception($errorInfo[2]);
|
|
|
+
|
|
|
+ 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.
|
|
|
// In order to do so we need to find the value for the key we're using
|
|
|
$id=$PDO->lastInsertId();
|
|
|
- $key=$PDO->query("SHOW INDEX FROM `{$this->_table}` WHERE Key_name='PRIMARY'")->fetch()['Column_name'];
|
|
|
- $this->_id=$PDO->query("SELECT `{$this->_key}` FROM `{$this->_table}` WHERE `$key`=$id")->fetchColumn();
|
|
|
+ if ($id!=0 && count($this->_keys)==0){
|
|
|
+ $this->_keys=$this->_ids=array();
|
|
|
+ $key=$PDO->query("SHOW INDEX FROM `{$this->_table}` WHERE Key_name='PRIMARY'")->fetch()['Column_name'];
|
|
|
+ $this->_keys[]=$key;
|
|
|
+ $this->_ids[]=$PDO->query("SELECT `$key` FROM `{$this->_table}` WHERE `$key`=$id")->fetchColumn();
|
|
|
+ $this->ReIndexKeyAndIdArrays();
|
|
|
+ }
|
|
|
$this->Load();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
}
|