public function getInstance() { if (null === self::$_instance) { self::$_instance = new self(); } return self::$_instance; }
public function save($bRefresh = false) { $objConnection = App_Db_Connection::getInstance()->get(); $objConnection->beginTransaction(); $strTableName = strtolower(get_class($this)); $arrData = get_object_vars($this); $arrAvailableFields = $this->_getFields(); $arrBind = []; $arrSqlPart = []; foreach ($arrData as $strField => $strValue) { if (!in_array($strField, $arrAvailableFields)) { continue; } $arrBind[":{$strField}"] = $strValue; array_push($arrSqlPart, "{$strField} = :{$strField}"); } if (empty($this->id)) { App_Db::write("INSERT {$strTableName} SET " . implode(", ", $arrSqlPart), $arrBind); } else { App_Db::write("UPDATE {$strTableName} SET " . implode(", ", $arrSqlPart) . " WHERE id = :id", $arrBind); } if (empty($this->id)) { $this->id = $objConnection->lastInsertId(); } $objConnection->commit(); if ($bRefresh) { $arrRow = App_Db::read("SELECT * FROM {$strTableName} WHERE id = :id", [":id" => $this->id])->fetch(PDO::FETCH_ASSOC); foreach ($arrRow as $strField => $strValue) { $this->{$strField} = $strValue; } } return $this->id; }
public static function write($strQuery, $arrBind = []) { self::addQuote($arrBind); $objConn = App_Db_Connection::getInstance()->get(); $objStatement = $objConn->prepare($strQuery); $objStatement->execute($arrBind); return $objConn; }