exemple
This commit is contained in:
parent
1c665f0125
commit
731ca85650
2
User.php
2
User.php
|
@ -92,6 +92,8 @@ class User
|
|||
*/
|
||||
public function getVotes()
|
||||
{
|
||||
$this->votes = VoteDao::create()->findByUser($this);
|
||||
|
||||
return $this->votes;
|
||||
}
|
||||
}
|
||||
|
|
25
Vote.php
25
Vote.php
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
29
VoteDao.php
29
VoteDao.php
|
@ -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']));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue