96 lines
2.0 KiB
PHP
96 lines
2.0 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Class VoteDao
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
*/
|
|
class VoteDao
|
|
{
|
|
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 findByArticle(Article $article)
|
|
{
|
|
$query = $this->pdo->prepare('select * from vote where article_id=:article_id');
|
|
|
|
$query->execute([
|
|
':article_id' => $article->getId(),
|
|
]);
|
|
|
|
$results = $query->fetchAll();
|
|
$datas = [];
|
|
|
|
foreach ($results as $result) {
|
|
$datas[] = $this->hydrate($result);
|
|
}
|
|
|
|
return $datas;
|
|
}
|
|
|
|
public function findByUser(User $user)
|
|
{
|
|
$query = $this->pdo->prepare('select * from vote where user_id=:user_id');
|
|
|
|
$query->execute([
|
|
':user_id' => $user->getId(),
|
|
]);
|
|
|
|
$results = $query->fetchAll();
|
|
$datas = [];
|
|
|
|
foreach ($results as $result) {
|
|
$datas[] = $this->hydrate($result);
|
|
}
|
|
|
|
return $datas;
|
|
}
|
|
|
|
public function findById($id)
|
|
{
|
|
$query = $this->pdo->prepare('select * from vote where id=:id');
|
|
|
|
$query->execute([
|
|
':id' => $id,
|
|
]);
|
|
|
|
$results = $query->fetch();
|
|
|
|
if (!empty($results)) {
|
|
return $this->hydrate($result);
|
|
}
|
|
}
|
|
|
|
public function hydrate(array $datas)
|
|
{
|
|
$vote = new Vote();
|
|
|
|
if (isset($datas['id'])) {
|
|
$vote->setId($datas['id']);
|
|
}
|
|
|
|
if (isset($datas['value'])) {
|
|
$vote->setValue($datas['value']);
|
|
}
|
|
|
|
if (isset($datas['article_id'])) {
|
|
$vote->setArticle(ArticleDao::create()->findById($datas['article_id']));
|
|
}
|
|
|
|
if (isset($datas['user_id'])) {
|
|
$vote->setUser(UserDao::create()->findById($datas['article_id']));
|
|
}
|
|
|
|
return $vote;
|
|
}
|
|
}
|
|
|