exemple
This commit is contained in:
parent
417cf9dc86
commit
1c665f0125
|
@ -1,3 +1,57 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class ArticleDao
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
26
User.php
26
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
|
||||
|
|
53
UserDao.php
53
UserDao.php
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class UserDao
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -55,7 +55,7 @@ class VoteDao
|
|||
public function hydrate(array $datas)
|
||||
{
|
||||
$vote = new Vote();
|
||||
zR
|
||||
|
||||
if (isset($datas['id'])) {
|
||||
$vote->setId($datas['id']);
|
||||
}
|
||||
|
|
13
autoload.php
Normal file
13
autoload.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
function __autoload($name)
|
||||
{
|
||||
static $classes = [];
|
||||
|
||||
if (isset($classes[$name])) {
|
||||
return;
|
||||
}
|
||||
|
||||
require __DIR__.'/'.$name.'.php';
|
||||
}
|
||||
|
Loading…
Reference in a new issue