Created
August 11, 2010 18:43
-
-
Save thiagosf/519502 to your computer and use it in GitHub Desktop.
Classes para conexão com banco de dados e Modelo para manipulação em PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Debug | |
function pr ($array) { | |
echo '<pre>'; | |
print_r($array); | |
echo '</pre>'; | |
} | |
/** | |
* Database configuration | |
*/ | |
class DATABASE_CONFIG | |
{ | |
public static $config = array( | |
'driver' => 'mysql', | |
'host' => 'localhost', | |
'login' => 'root', | |
'password' => '', | |
'database' => 'teste', | |
'encoding' => 'utf8', | |
); | |
} | |
/** | |
* Object | |
*/ | |
class Object { } | |
/** | |
* Database | |
*/ | |
class Database extends Object | |
{ | |
private $con; | |
private static $driver = null; | |
public function __construct () { | |
$config = DATABASE_CONFIG::$config; | |
switch ($config['driver']) | |
{ | |
case 'mysql' : | |
$this->driver = new MySQL; | |
$this->con = $this->driver->connect( | |
$config['host'], | |
$config['login'], | |
$config['password'], | |
$config['database'] | |
); | |
break; | |
} | |
if (isset($config['encoding'])) { | |
$this->driver->query(sprintf('SET NAMES `%s`', $config['encoding'])); | |
} | |
} | |
public static function open () { | |
if (!self::$driver) { | |
self::$driver = new DataBase; | |
} | |
return self::$driver; | |
} | |
public function query ($sql) { | |
return $this->driver->query($sql); | |
} | |
public function fetch_assoc ($query) { | |
return $this->driver->fetch_assoc($query); | |
} | |
} | |
/** | |
* Drivers | |
*/ | |
abstract class Drivers extends Object | |
{ | |
abstract function connect ($host, $username, $password, $database); | |
abstract function query ($query); | |
abstract function fetch_assoc ($query); | |
} | |
/** | |
* MySQL | |
*/ | |
class MySQL extends Drivers | |
{ | |
public function connect ($host, $username, $password, $database) { | |
$con = mysql_connect($host, $username, $password); | |
mysql_select_db($database); | |
return $con; | |
} | |
public function query ($sql) { | |
return mysql_query($sql); | |
} | |
public function fetch_assoc ($query) { | |
return mysql_fetch_assoc($query); | |
} | |
} | |
/** | |
* Model | |
*/ | |
class Model | |
{ | |
private $database; | |
protected $table; | |
protected $fields = '*'; | |
protected $cond; | |
public function __construct () { | |
$this->database = Database::open(); | |
$this->setTable(); | |
} | |
private function setTable () { | |
if (!$this->table) { | |
$this->table = strtolower(get_class($this)); | |
} | |
} | |
public function all ($conditions = null) { | |
$sql = sprintf('SELECT %s FROM `%s` %s', $this->fields, $this->table, $conditions); | |
$query = $this->database->query($sql); | |
$data = array(); | |
while ($line = $this->database->fetch_assoc($query)) { | |
$data[] = $line; | |
} | |
return $data; | |
} | |
public function __call ($function, $args) { | |
if (preg_match('/^(findAllBy)(.*)/', $function, $matches)) { | |
$field = $matches[2]; | |
$field = preg_replace('/([A-Z])/', '_\\0', $field); | |
$field = strtolower(substr($field, 1)); | |
$cond = sprintf('WHERE `%s` = "%s"', $field, $args[0]); | |
return $this->all($cond); | |
} | |
} | |
} | |
/** | |
* Artigos | |
*/ | |
class Artigos extends Model { } | |
// Examples | |
$artigos = new Artigos; | |
$data = $artigos->all(); | |
$data2 = $artigos->findAllByIdCategoria(2); | |
pr($data); | |
pr($data2); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment