@ 2014-01-21T19:07:44Z <?php
abstract class Model {
/**
* Tabela representativa.
* @var [array]
*/
private $_Table;
/**
* Meta-Descrições dos Campos.
* @var [array]
*/
protected $_Meta;
/**
* Listagem dos Campos.
* @var [array]
*/
protected $_Fields;
/**
* Listagem de Campos que não são podem ser Nulos.
* @var [array]
*/
protected $_NotNull;
/**
* Instância de banco de dados Singleton.
* @var [PDO]
*/
protected static $db;
/**
* Listagem de todas as instâncias.
* @var [array][array]
*/
private static $_Cache;
public static function _Instance() {
self::$db = new db();
}
/**
* Define a tabela que será usada no objeto.
* @param [string] $table Tabela representativa.
*/
private function setTable($table) {
# Verifica se a entrada é válida.
if(empty($table))
throw new Exception("A tabela não pode ser vazia.");
# Define a tabela na classe.
$this->_Table = $table;
}
public function Insert() {
try
{
Validate::Run($this);
foreach($this->_Fields as $Field)
$binds[':' . $Field] = $this->{$Field};
$values = implode(', ', keys($binds));
$fields = $this->_Fields;
unset($fields[0]);
$fields = implode(', ', $this->_Fields);
$query = "INSERT INTO ". $this->_Table ."(". $fields .") VALUES(". $values .")";
self::e($query, $binds);
$this->Select(self::$db->lastInsertId());
}
catch(Exception $e)
{
echo $e->getMessage();
return false;
}
}
public function Select($id = NULL) {
if(is_null($id)) {
$id = $this->{$this->_Fields[0]};
if(is_null($id))
throw new Exception("Impossível realizar select com id nulo da tabela ". $this->_Table .".");
}
$fields = implode(', ', $this->_Fields);
$query = "SELECT ". $fields ." FROM ". $this->_Table ." WHERE ". $this->_Fields[0] ." = ?";
$stmt = self::e($query , array($id));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($row) {
$this->_ResetFields();
foreach($this->_Fields as $Field)
$this->{$Field} = $row[$Field];
# $this->{'Hash'} = Bcrypt::hash($id . $this->_Table);
self::$_Cache[$this->_Table][$this->_Fields[0]] = $this;
}
else
throw new Exception("Id ". $id ." não existe na tabela ". $this->_Table);
}
public function Delete($id = NULL) {
if(is_null($id)) {
$id = $this->{$this->_Fields[0]};
if(is_null($id))
throw new Exception("Impossível deletar um id nulo da tabela ". $this->_Table .".");
}
$query = "SELECT ". $fields ." FROM ". $this->_Table ." WHERE ". $this->_Fields[0] ." = ?";
$stmt = self::e($query , array($id));
if($stmt->fetch()) {
if(Db::$VirtualDeletion['on']) {
$query = "UPDATE ". $this->_Table ." SET ". Db::$VirtualDeletion['Field'] ." = '". Db:VirtualDeletion['Value'] ."' WHERE ". $this->_Fields[0] ." = ?";
self::e($query, array($id));
}
else {
$query = "DELETE FROM ". $this->_Table ." WHERE ". $this->_Fields[0] ." = ?";
self::e($query, array($id));
}
return true;
}
else
throw new Exception("O id ". $id ." não pode ser excluído da tabela ". $this->_Table ." pois não existe.");
}
public function Update($id = NULL) {
if(is_null($id)) {
$id = $this->{$this->_Fields[0]};
if(is_null($id))
throw new Exception("Impossível realizer update em um id nulo da tabela ". $this->_Table .".");
}
Validate::Run($this);
foreach($this->_Fields as $Field)
$binds[':' . $Field] = $this->{$Field};
$fields = $this->_Fields;
unset($fields[0]);
foreach($fields as $Field)
$values[] = $Field .' = '. $this->{$Field};
$values[] = implode(', ', $values);
$query = "UPDATE ". $this->_Table ." SET ". $values ." WHERE ". $this->_Fields[0] ." = :" . $this->_Fields[0];
self::e($query, $binds);
unset(
self::$_Cache[$this->_Table][$this->_Fields[0]]
);
$this->Select($id);
}
/**
* Limpa os objetos da memória cache.
*/
public static function Clear() {
unset(self::$_Cache);
}
protected static function e($query, $binds) {
$_stmt = self::$db->prepare($query);
$_stmt->execute($binds);
return $_stmt;
}
protected static function q($query) {
return self::$db->query($query);
}
public function __destruct() {
self::$db = null;
unset(self::$_Cache);
}
}
?>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 5.5.8 0.010 0.077 17.33 5.5.7 0.017 0.067 17.31 5.5.6 0.000 0.047 17.24 5.5.5 0.010 0.073 17.22 5.5.4 0.023 0.030 17.18 5.5.3 0.003 0.043 17.18 5.5.2 0.010 0.077 17.19 5.5.1 0.007 0.067 17.21 5.5.0 0.010 0.070 17.23 5.4.24 0.007 0.057 19.04 5.4.23 0.010 0.063 18.88 5.4.22 0.003 0.043 19.12 5.4.21 0.007 0.040 19.00 5.4.20 0.007 0.077 19.10 5.4.19 0.010 0.050 19.09 5.4.18 0.007 0.073 19.04 5.4.17 0.003 0.053 19.02 5.4.16 0.013 0.047 19.12 5.4.15 0.003 0.043 18.99 5.4.14 0.000 0.040 16.31 5.4.13 0.007 0.047 16.31 5.4.12 0.013 0.060 16.30 5.4.11 0.010 0.073 16.26 5.4.10 0.010 0.077 16.44 5.4.9 0.010 0.070 16.32 5.4.8 0.000 0.057 16.41 5.4.7 0.003 0.047 16.44 5.4.6 0.010 0.073 16.48 5.4.5 0.007 0.060 16.50 5.4.4 0.003 0.073 16.41 5.4.3 0.000 0.057 16.39 5.4.2 0.007 0.033 16.37 5.4.1 0.007 0.050 16.38 5.4.0 0.010 0.030 15.84 5.3.28 0.007 0.080 14.68 5.3.27 0.003 0.053 14.86 5.3.26 0.013 0.067 14.85 5.3.25 0.003 0.070 14.64 5.3.24 0.007 0.060 14.66 5.3.23 0.017 0.070 14.68 5.3.22 0.010 0.073 14.70 5.3.21 0.003 0.083 14.65 5.3.20 0.010 0.070 14.56 5.3.19 0.003 0.087 14.63 5.3.18 0.000 0.080 14.65 5.3.17 0.007 0.043 14.62 5.3.16 0.000 0.077 14.55 5.3.15 0.007 0.043 14.62 5.3.14 0.007 0.050 14.63 5.3.13 0.007 0.047 14.54 5.3.12 0.000 0.053 14.59 5.3.11 0.007 0.077 14.80 5.3.10 0.010 0.070 14.02 5.3.9 0.003 0.073 14.03 5.3.8 0.007 0.047 14.03 5.3.7 0.007 0.080 14.04 5.3.6 0.000 0.053 14.04 5.3.5 0.013 0.060 14.20 5.3.4 0.000 0.080 13.87 5.3.3 0.007 0.053 14.00 5.3.2 0.007 0.070 13.83 5.3.1 0.003 0.053 13.73 5.3.0 0.003 0.037 13.55
preferences:dark mode live preview
143.25 ms | 1394 KiB | 7 Q