diff --git a/hw12/index.php b/hw12/index.php new file mode 100644 index 000000000..c6200bedf --- /dev/null +++ b/hw12/index.php @@ -0,0 +1,14 @@ +getMessage(); +} diff --git a/hw12/src/controller/UserController.php b/hw12/src/controller/UserController.php new file mode 100644 index 000000000..ce4b23af9 --- /dev/null +++ b/hw12/src/controller/UserController.php @@ -0,0 +1,48 @@ +mapper = new UserMapper($host, $database); + } + + public function insertUser($params) { + try { + $this->mapper->insert($params['username'], $params['phone']); + } catch (Exception $e) { + return $e->getMessage(); + } + } + + public function updateUser($params) { + try { + $this->mapper->update($params); + } catch (Exception $e) { + return $e->getMessage(); + } + } + + public function deleteUser(User $user) { + try { + $this->mapper->delete($user); + } catch (Exception $e) { + return $e->getMessage(); + } + } + + public function selectUserById($id) { + try { + $this->mapper->findById($id); + } catch (Exception $e) { + return $e->getMessage(); + } + } +} \ No newline at end of file diff --git a/hw12/src/model/User.php b/hw12/src/model/User.php new file mode 100644 index 000000000..527c6a603 --- /dev/null +++ b/hw12/src/model/User.php @@ -0,0 +1,32 @@ +username = $username; + $this->phone = $phone; + } + + public function getId() + { + return $this->id; + } + + public function getUsername() + { + return $this->username; + } + + public function getPhone() + { + return $this->phone; + } +} \ No newline at end of file diff --git a/hw12/src/model/UserMapper.php b/hw12/src/model/UserMapper.php new file mode 100644 index 000000000..91efc7052 --- /dev/null +++ b/hw12/src/model/UserMapper.php @@ -0,0 +1,88 @@ +connection = new PDO("mysql:host=$host;dbname=$database"); + } + + + /** + * Создание нового пользователя. + * + * @param string $username + * @param int $phone + */ + public function insert($username, $phone) + { + return new User($username, $phone); + } + + /** + * Обновление пользователя. + * + * @param array $params + */ + public function update($params) + { + $user = $this->findById($params['id']); + if ($params['username'] && $params['username'] !== $user->getUsername()) { + $query = 'UPDATE users SET username=? WHERE id=?'; + $this->connection->query($query, + array( + $params['username'], + $user->getId() + )); + } + if ($params['phone'] && $params['phone'] !== $user->getPhone()) { + $query = 'UPDATE users SET phone=? WHERE id=?'; + $this->connection->query($query, + array( + $params['phone'], + $user->getId() + )); + } + } + + /** + * Удаление пользователя из бд. + * + * @param User $user + */ + public function delete(User $user) + { + $query = 'DELETE FROM users WHERE id = ?'; + $this->connection->query($query, array($user->getId())); + } + + /** + * Поиск пользователя по id. + * + * @param int $userId + * @return User|null + */ + public function findById($userId) + { + $sql = "SELECT id, username, phone FROM users WHERE id = ?"; + + $data = $this->connection->fetchRow($sql, array($userId), Zend_Db::FETCH_ASSOC); + + $user = null; + + if ($data != false) + { + $user = $this->insert($data); + } + + return $user; + } +} \ No newline at end of file