diff --git a/ArticleDao.php b/ArticleDao.php index 50cce95..57f96ea 100644 --- a/ArticleDao.php +++ b/ArticleDao.php @@ -1,3 +1,57 @@ + */ +class ArticleDao +{ + 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 findById($id) + { + $query = $this->pdo->prepare('select * from article where id=:id'); + + $query->execute([ + ':id' => $id, + ]); + + $results = $query->fetch(); + + if (!empty($results)) { + return $this->hydrate($results); + } + + return null; + } + + public function hydrate(array $datas) + { + $article = new Article(); + + if (isset($datas['id'])) { + $article->setId($datas['id']); + } + + if (isset($datas['title'])) { + $article->setTitle($datas['title']); + } + + if (isset($datas['content'])) { + $article->setContent($datas['content']); + } + + return $article; + } +} diff --git a/User.php b/User.php index a8a9bf9..248672b 100644 --- a/User.php +++ b/User.php @@ -11,6 +11,11 @@ class User */ protected $id; + /** + * @var string + */ + protected $username; + /** * @var array $votes; */ @@ -34,6 +39,27 @@ class User { return $this->id; } + + + /** + * @param string $username + * @return + */ + public function setUsername($username) + { + $this->username = (string) $username; + + return $this; + } + + /** + * @return string $username + */ + public function getUsername() + { + return $this->username; + } + /** * @param Vote $vote diff --git a/UserDao.php b/UserDao.php index e69de29..8ba37f9 100644 --- a/UserDao.php +++ b/UserDao.php @@ -0,0 +1,53 @@ + + */ +class UserDao +{ + 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 findById($id) + { + $query = $this->pdo->prepare('select * from user where id=:id'); + + $query->execute([ + ':id' => $id, + ]); + + $results = $query->fetch(); + + if (!empty($results)) { + return $this->hydrate($results); + } + + return null; + } + + public function hydrate(array $datas) + { + $article = new User(); + + if (isset($datas['id'])) { + $article->setId($datas['id']); + } + + if (isset($datas['username'])) { + $article->setUsername($datas['username']); + } + + return $article; + } +} + diff --git a/VoteDao.php b/VoteDao.php index 496bbdd..dd8006a 100644 --- a/VoteDao.php +++ b/VoteDao.php @@ -55,7 +55,7 @@ class VoteDao public function hydrate(array $datas) { $vote = new Vote(); -zR + if (isset($datas['id'])) { $vote->setId($datas['id']); } diff --git a/autoload.php b/autoload.php new file mode 100644 index 0000000..4f14a26 --- /dev/null +++ b/autoload.php @@ -0,0 +1,13 @@ +findById(1); +$votes = $articleFoo->getVotes(); + +var_dump($votes);