Created
May 10, 2013 00:08
-
-
Save kinncj/5551558 to your computer and use it in GitHub Desktop.
PDO and it's magics - PHP Brasil - May 9, 2013
This file contains hidden or 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
{"driver":"mysql","server":"localhost","database":"pdoteste","username":"user","password":"password"} |
This file contains hidden or 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 | |
class Db | |
{ | |
private $pdo; | |
protected $table; | |
public function __construct() | |
{ | |
$parameters = json_decode(file_get_contents('config.json')); | |
$dsn = "{$parameters->driver}:host={$parameters->server};dbname={$parameters->database}"; | |
$this->pdo = new PDO($dsn, $parameters->username, $parameters->password, array(PDO::ATTR_PERSISTENT => true)); | |
} | |
/** | |
* Cria a query | |
* | |
* @param array $fieldsParameter Array de parametros array("nome", "idade"); | |
* @param array $whereParameter Array de parametros para cláusula where array(":nome"=>"xuplau") | |
*/ | |
public function query(array $fieldsParameter = array('*'), array $whereParameter = array()) | |
{ | |
$rawQuery = "SELECT %FIELDS% FROM {$this->table} %WHERE%"; | |
$fields = null; | |
$where = null; | |
foreach ($fieldsParameter as $field) { | |
$fields .= "{$field},"; | |
} | |
$fields = substr($fields, 0, -1); | |
foreach ($whereParameter as $bind=>$value) { | |
$field = substr($bind, 1); | |
$where .= " {$field} = {$bind} AND"; | |
} | |
if ( ! is_null($where)) { | |
$where = strpos("WHERE {$where}",0 -3); | |
} | |
$query = str_replace(array("%FIELDS%", "%WHERE%"), array($fields, $where), $rawQuery); | |
$statement = $this->pdo->prepare($query); | |
$statement->execute($whereParameter); | |
return $statement; | |
} | |
} |
This file contains hidden or 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 | |
spl_autoload_register(function($class){ | |
require_once "{$class}.php"; | |
}); | |
$UserDao = new UserDao(); | |
$userList = $UserDao->findAll(); | |
foreach ($userList as $user) { | |
$user->falarNome().PHP_EOL; | |
} |
This file contains hidden or 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
CREATE DATABASE pdoteste; | |
USE pdoteste; | |
CREATE TABLE users(id int autoincrement NOT NULL, nome text); | |
INSET INTO users(nome) VALUES("Foo"),("Bar"),("Baz"); |
This file contains hidden or 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 | |
class User | |
{ | |
private $name; | |
public function falarNome() | |
{ | |
echo "Ola, sou o {$this->nome}"; | |
} | |
} |
This file contains hidden or 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 | |
class UserDao extends Db | |
{ | |
protected $table = 'users'; | |
public function findAll() | |
{ | |
$result = $this->query(); | |
$userList = array(); | |
while($user = $result->fetchObject("User")) { | |
$userList[] = $user; | |
} | |
return $userList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
E na real, o UserDao.php ta mais para um repository, sei la