58 lines
1.1 KiB
PHP
58 lines
1.1 KiB
PHP
<?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;
|
|
}
|
|
}
|
|
|