This commit is contained in:
Simon Vieille 2016-05-14 11:17:33 +02:00
parent 1c665f0125
commit 731ca85650
4 changed files with 64 additions and 7 deletions

View file

@ -92,6 +92,8 @@ class User
*/
public function getVotes()
{
$this->votes = VoteDao::create()->findByUser($this);
return $this->votes;
}
}

View file

@ -16,6 +16,11 @@ class Vote
*/
protected $user;
/**
* @var string
*/
protected $value;
/**
* @param Article $article
@ -62,5 +67,25 @@ class Vote
{
return $this->user;
}
/**
* @param string $value
* @return
*/
public function setValue($value)
{
$this->value = (string) $value;
return $this;
}
/**
* @return string $value
*/
public function getValue()
{
return $this->value;
}
}

View file

@ -36,19 +36,36 @@ class VoteDao
return $datas;
}
public function findById(Article $article)
public function findByUser(User $user)
{
$query = $this->pdo->prepare('select * from vote where article_id=:article_id');
$query = $this->pdo->prepare('select * from vote where user_id=:user_id');
$query->execute([
':article_id' => $article->getId(),
':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);
}
}
@ -60,6 +77,10 @@ class VoteDao
$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']));
}

View file

@ -3,6 +3,15 @@
require __DIR__.'/autoload.php';
$articleFoo = ArticleDao::create()->findById(1);
$votes = $articleFoo->getVotes();
var_dump($votes);
foreach ($votes as $vote) {
$user = $vote->getUser();
echo $user->getUsername();
$votes = $user->getVotes();
var_dump($votes);
}