54 lines
989 B
PHP
54 lines
989 B
PHP
<?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;
|
|
}
|
|
}
|
|
|