This commit is contained in:
Simon Vieille 2016-05-14 10:58:10 +02:00
parent 417cf9dc86
commit 1c665f0125
6 changed files with 155 additions and 1 deletions

View file

@ -1,3 +1,57 @@
<?php
/**
* Class ArticleDao
* @author Simon Vieille <simon@deblan.fr>
*/
class ArticleDao
{
protected $pdo;
public function __construct()
{
$this->pdo = new Pdo('mysql:dbname=test;host=127.0.0.1', 'root', 'root');
}
public static function create()
{
return new static();
}
public function findById($id)
{
$query = $this->pdo->prepare('select * from article where id=:id');
$query->execute([
':id' => $id,
]);
$results = $query->fetch();
if (!empty($results)) {
return $this->hydrate($results);
}
return null;
}
public function hydrate(array $datas)
{
$article = new Article();
if (isset($datas['id'])) {
$article->setId($datas['id']);
}
if (isset($datas['title'])) {
$article->setTitle($datas['title']);
}
if (isset($datas['content'])) {
$article->setContent($datas['content']);
}
return $article;
}
}

View file

@ -11,6 +11,11 @@ class User
*/
protected $id;
/**
* @var string
*/
protected $username;
/**
* @var array $votes;
*/
@ -34,6 +39,27 @@ class User
{
return $this->id;
}
/**
* @param string $username
* @return
*/
public function setUsername($username)
{
$this->username = (string) $username;
return $this;
}
/**
* @return string $username
*/
public function getUsername()
{
return $this->username;
}
/**
* @param Vote $vote

View file

@ -0,0 +1,53 @@
<?php
/**
* Class UserDao
* @author Simon Vieille <simon@deblan.fr>
*/
class UserDao
{
protected $pdo;
public function __construct()
{
$this->pdo = new Pdo('mysql:dbname=test;host=127.0.0.1', 'root', 'root');
}
public static function create()
{
return new static();
}
public function findById($id)
{
$query = $this->pdo->prepare('select * from user where id=:id');
$query->execute([
':id' => $id,
]);
$results = $query->fetch();
if (!empty($results)) {
return $this->hydrate($results);
}
return null;
}
public function hydrate(array $datas)
{
$article = new User();
if (isset($datas['id'])) {
$article->setId($datas['id']);
}
if (isset($datas['username'])) {
$article->setUsername($datas['username']);
}
return $article;
}
}

View file

@ -55,7 +55,7 @@ class VoteDao
public function hydrate(array $datas)
{
$vote = new Vote();
zR
if (isset($datas['id'])) {
$vote->setId($datas['id']);
}

13
autoload.php Normal file
View file

@ -0,0 +1,13 @@
<?php
function __autoload($name)
{
static $classes = [];
if (isset($classes[$name])) {
return;
}
require __DIR__.'/'.$name.'.php';
}

8
index.php Normal file
View file

@ -0,0 +1,8 @@
<?php
require __DIR__.'/autoload.php';
$articleFoo = ArticleDao::create()->findById(1);
$votes = $articleFoo->getVotes();
var_dump($votes);