diff --git a/User.php b/User.php index 248672b..3dc6902 100644 --- a/User.php +++ b/User.php @@ -92,6 +92,8 @@ class User */ public function getVotes() { + $this->votes = VoteDao::create()->findByUser($this); + return $this->votes; } } diff --git a/Vote.php b/Vote.php index 9d9cd89..970cadd 100644 --- a/Vote.php +++ b/Vote.php @@ -15,6 +15,11 @@ class Vote * @var User */ protected $user; + + /** + * @var string + */ + protected $value; /** @@ -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; + } } diff --git a/VoteDao.php b/VoteDao.php index dd8006a..d9e0a4d 100644 --- a/VoteDao.php +++ b/VoteDao.php @@ -35,20 +35,37 @@ 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'])); } @@ -67,7 +88,7 @@ class VoteDao if (isset($datas['user_id'])) { $vote->setUser(UserDao::create()->findById($datas['article_id'])); } - + return $vote; } } diff --git a/index.php b/index.php index 5588587..43ae56d 100644 --- a/index.php +++ b/index.php @@ -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); +}