Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class UserRepository implements UserRepositoryInterface
- {
- private $_db;
- private $_mysql_host = 'localhost';
- private $_port = '3306';
- private $_username = 'root';
- private $_password = '';
- private $_database = 'reservationsystem';
- public function UserRepository()
- {
- try
- {
- $this->_db = new PDO('mysql:host='.$this->_mysql_host.';dbname='.$this->_database.';port='.$this->_port, $this->_username, $this->_password);
- $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch(PDOException $e)
- {
- echo 'Połączenie nie mogło zostać nawiązane.<br>' . $e->getMessage();
- }
- }
- public function getUser(User $user)
- {
- try
- {
- $query = $this->_db->prepare('SELECT id FROM `Users` WHERE `fullname` = :fullname AND `login` = :login AND `password` = :password AND `phone` = :phone AND `email` = :email');
- @$query->bindParam(':fullname', $user->getFullName(), PDO::PARAM_STR);
- @$query->bindParam(':login', $user->getLogin(), PDO::PARAM_STR);
- @$query->bindParam(':password', $user->getPassword(), PDO::PARAM_STR);
- @$query->bindParam(':phone', $user->getPhone(), PDO::PARAM_STR);
- @$query->bindParam(':email', $user->getEmail(), PDO::PARAM_STR);
- $query->execute();
- return $query->fetch();
- }
- catch (PDOException $e)
- {
- echo $e->getMessage();
- }
- }
- public function getUserId(User $user)
- {
- return $this->getUser($user)['id'];
- }
- public function getByFullname($fullname)
- {
- }
- public function getById($id)
- {
- }
- public function exists(User $user)
- {
- if (empty($this->getUser($user)))
- return false;
- else
- return true;
- }
- public function save(User $user)
- {
- try
- {
- if ($this->exists($user))
- return;
- $query = $this->_db->prepare('INSERT INTO `Users` (`fullname`,`login`,`password`,`phone`,`email`) VALUES(:fullname, :login, :password, :phone, :email)');
- @$query->bindParam(':fullname', $user->getFullName(), PDO::PARAM_STR);
- @$query->bindParam(':login', $user->getLogin(), PDO::PARAM_STR);
- @$query->bindParam(':password', $user->getPassword(), PDO::PARAM_STR);
- @$query->bindParam(':phone', $user->getPhone(), PDO::PARAM_STR);
- @$query->bindParam(':email', $user->getEmail(), PDO::PARAM_STR);
- $query->execute();
- }
- catch(PDOException $e)
- {
- echo 'Wystąpił błąd przy dodawaniu użytkownika <br>' .$e->getMessage();
- }
- }
- public function remove(User $user)
- {
- try
- {
- if (!$this->exists($user))
- return;
- $query = $this->_db->prepare('DELETE FROM `Users` WHERE `id` = :id');
- @$query->bindParam(':id', $user->getId(), PDO::PARAM_INT);
- $query->execute();
- }
- catch(PDOException $e)
- {
- echo 'Wystąpił błąd przy usuwaniu użytkownika <br>' .$e->getMessage();
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement