From a1f99892af0848c6fdb1da42605f79c2f66294a1 Mon Sep 17 00:00:00 2001 From: sundowndev Date: Sun, 22 Jul 2018 02:33:49 +0200 Subject: [PATCH] User repository --- server/src/Repository/UserRepository.php | 74 ++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 server/src/Repository/UserRepository.php diff --git a/server/src/Repository/UserRepository.php b/server/src/Repository/UserRepository.php new file mode 100644 index 0000000..09f2d87 --- /dev/null +++ b/server/src/Repository/UserRepository.php @@ -0,0 +1,74 @@ +db = $db; + $this->tableName = 'User'; + } + + /** + * @return mixed + */ + public function findAll() + { + $stmt = $this->db->getConnection()->prepare('SELECT * FROM ' . $this->tableName . ' ORDER BY id DESC'); + $stmt->execute(); + + return $stmt->fetchAll(\PDO::FETCH_ASSOC); + } + + /** + * @param $id + * @return null + */ + public function findOneById($id) + { + $stmt = $this->db->getConnection()->prepare('SELECT * FROM ' . $this->tableName . ' WHERE id = :id'); + $stmt->bindParam(':id', $id, \PDO::PARAM_INT); + $stmt->execute(); + + $user = $stmt->fetch(\PDO::FETCH_ASSOC); + + if (!$user) { + return null; + } else { + return $user; + } + } + + public function findOneByUsername($username) + { + $stmt = $this->db->getConnection()->prepare('SELECT * FROM ' . $this->tableName . ' WHERE name = :username'); + $stmt->bindParam(':username', $username, \PDO::PARAM_INT); + $stmt->execute(); + + $user = $stmt->fetch(\PDO::FETCH_ASSOC); + + if (!$user) { + return null; + } else { + return $user; + } + } +} \ No newline at end of file