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
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
33
VoteDao.php
33
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;
|
||||
}
|
||||
}
|
||||
|
|
11
index.php
11
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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue