Last active
April 6, 2017 23:31
-
-
Save ronaldooliveiradevbr/06c0c4dd27fcd86fba9fe84cf01e110a to your computer and use it in GitHub Desktop.
Dúvida refactor DataMapper
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 | |
// Isso é o que tem hoje | |
class Random | |
{ | |
// Este é o único atributo que essas classes random tem =( | |
private $db; | |
public function __construct(Db $db) | |
{ | |
$this->db = $db; | |
} | |
// Users | |
public function getUserById($id) | |
{ | |
$sql = "SELECT * FROM pqp_users WHERE id = :id"; // Pra ser sistema legado, a table tem que ter prefixo HUE... | |
$stmt = $this->db->prepare($sql); | |
if (! $stmt->execute(["id" => $id])) { | |
throw new RuntimeExeption("Fuuu.."); | |
} | |
$result = $stmt->fetch(); | |
return $result; | |
} | |
// Posts (WTF?!) | |
public function getRecentPosts($limit) | |
{ | |
$sql = "SELECT * FROM pqp_posts ORDER BY created_at DESC LIMIT {$limit}"; // Pra ser sistema legado, a table tem que ter prefixo HUE... | |
$stmt = $this->db->prepare($sql); | |
if (! $stmt->execute()) { | |
throw new RuntimeExeption("Fuuu.."); | |
} | |
$result = $stmt->fetchAll(); | |
return $result; | |
} | |
} |
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 | |
class User | |
{ | |
private $id; | |
private $name; | |
private $email; | |
public function __construct($name, $email) | |
{ | |
$this->name = $name; | |
$this->email = $email; | |
} | |
/* GETTERS E SETTERS */ | |
} | |
class UserMapper | |
{ | |
private $pdo; | |
public function __construct(PDO $pdo) | |
{ | |
$this->pdo = $pdo | |
} | |
public function getById($id) | |
{ | |
$stmt = $this->pdo->prepare( | |
"SELECT * | |
FROM pqp_users | |
WHERE id = :id" | |
); | |
$stmt->bindParam(":id", $id, PDO::PARAM_INT); | |
$stmt->setFetchMode(PDO::FETCH_CLASS|PDO::PROPS_LATE, "User"); | |
if (! $stmt->execute()) { | |
// Exception... | |
} | |
return $stmt->fetch(); | |
} | |
} | |
// Exemplo de uso | |
try { | |
$pdo = new PDO(...); | |
} catch (PDOException $e) { | |
// ... | |
} | |
$userMapper = new UserMapper($pdo); | |
$user = $userMapper->getById(1); | |
print_r($user); |
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 | |
// Faz sentido fazer isso??? | |
class Post | |
{ | |
private $id; | |
private $title; | |
private $body; | |
private $created_at; | |
private $updated_at; | |
public function __construct($title, $body) | |
{ | |
$this->title = $title; | |
$this->body = $body; | |
} | |
/* GETTERS E SETTERS */ | |
} | |
class PostMapper | |
{ | |
private $pdo; | |
public function __construct(PDO $pdo) | |
{ | |
$this->pdo = $pdo | |
} | |
public function getLastCreated($limit) | |
{ | |
$stmt = $this->pdo->prepare( | |
"SELECT * | |
FROM pqp_posts | |
ORDER BY created_at DESC | |
LIMIT {$limit}" | |
); | |
$stmt->setFetchMode(PDO::FETCH_CLASS|PDO::PROPS_LATE, "Post"); | |
if (! $stmt->execute()) { | |
// Exception... | |
} | |
return $stmt->fetchAll(); | |
} | |
} | |
//Exemplo | |
$postMapper = new PostMapper($pdo); | |
$recentPosts = $postMapper->getLastCreated(10); | |
print_r($recentPosts); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment