From 4ff0def140f9be10cd7ac6fad26da093370f65fe Mon Sep 17 00:00:00 2001 From: kbariotis Date: Sat, 11 May 2013 20:07:59 +0300 Subject: [PATCH 1/5] Added DataMapper files --- DataMapper/Test.php | 0 DataMapper/User.php | 73 +++++++++++++++++++++++++++++++++++++ DataMapper/UserMapper.php | 77 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 DataMapper/Test.php create mode 100644 DataMapper/User.php create mode 100644 DataMapper/UserMapper.php diff --git a/DataMapper/Test.php b/DataMapper/Test.php new file mode 100644 index 0000000..e69de29 diff --git a/DataMapper/User.php b/DataMapper/User.php new file mode 100644 index 0000000..a61b243 --- /dev/null +++ b/DataMapper/User.php @@ -0,0 +1,73 @@ +setOptions($options); + } + } + + public function __set($name, $value) + { + $method = 'set' . $name; + if (('mapper' == $name) || !method_exists($this, $method)) { + throw new Exception('Invalid Offer property'); + } + $this->$method($value); + } + + public function __get($name) + { + $method = 'get' . $name; + if (('mapper' == $name) || !method_exists($this, $method)) { + throw new Exception('Invalid Offer property'); + } + return $this->$method(); + } + + public function setOptions(array $options) + { + $methods = get_class_methods($this); + + foreach ($options as $key => $value) { + $method = 'set' . ucfirst($key); + + if (in_array($method, $methods)) { + $this->$method($value); + + } + } + return $this; + } + + public function getUserId() { + return $this->userId; + } + + public function setUserID($userId) { + $this->userId = $userId; + } + + public function getUsername() { + return $this->username; + } + + public function setUsername($username) { + $this->username = $username; + } + + public function getEmail() { + return $this->email; + } + + public function setEmail($email) { + $this->email = $email; + } + +} \ No newline at end of file diff --git a/DataMapper/UserMapper.php b/DataMapper/UserMapper.php new file mode 100644 index 0000000..69201fa --- /dev/null +++ b/DataMapper/UserMapper.php @@ -0,0 +1,77 @@ + $user->getUserId(), + 'username' => $user->getUsername(), + 'email' => $user->getEmail(), + ); + + if (null === ($id = $user->getUserId())) { + unset($data['userid']); + $this->_dbTable->insert($data); + } else { + $this->_dbTable->update($data, array('userid = ?' => $id)); + } + } + + // will find a user from db based on the id and return it to a User object in memory + public function find($id, User $user) + { + $result = $this->_dbTable()->find($id); + if (0 == count($result)) { + return; + } + $row = $result->current(); + + $user->setOptions($row); + } + + // will fetch all entries from a table to memory + public function fetchAll() + { + $resultSet = $this->_dbTable()->fetchAll(); + $entries = array(); + + foreach ($resultSet as $row) { + $entry = new User(); + $entry->setOptions($row); + $entries[] = $entry; + } + + return $entries; + } +} \ No newline at end of file From 73cd3bf9cd787fba6a229435f9c6f8664ddf9000 Mon Sep 17 00:00:00 2001 From: kbariotis Date: Mon, 13 May 2013 20:40:21 +0300 Subject: [PATCH 2/5] added DataMapper Example --- DataMapper/Test.php | 19 +++++++++++++ DataMapper/User.php | 41 ++++++-------------------- DataMapper/UserMapper.php | 60 ++++++++++++++++++++++++++++++--------- 3 files changed, 73 insertions(+), 47 deletions(-) diff --git a/DataMapper/Test.php b/DataMapper/Test.php index e69de29..7aba20e 100644 --- a/DataMapper/Test.php +++ b/DataMapper/Test.php @@ -0,0 +1,19 @@ +find(1); + + if ($user !== null) { + echo "Hello " . $user->getUsername() . ". Your email is " . $user->getEmail(); + } + +?> \ No newline at end of file diff --git a/DataMapper/User.php b/DataMapper/User.php index a61b243..8edfa69 100644 --- a/DataMapper/User.php +++ b/DataMapper/User.php @@ -1,5 +1,13 @@ $method($value); - } - - public function __get($name) - { - $method = 'get' . $name; - if (('mapper' == $name) || !method_exists($this, $method)) { - throw new Exception('Invalid Offer property'); - } - return $this->$method(); - } - - public function setOptions(array $options) - { - $methods = get_class_methods($this); - - foreach ($options as $key => $value) { - $method = 'set' . ucfirst($key); - - if (in_array($method, $methods)) { - $this->$method($value); - - } - } - return $this; - } - public function getUserId() { return $this->userId; } diff --git a/DataMapper/UserMapper.php b/DataMapper/UserMapper.php index 69201fa..0688564 100644 --- a/DataMapper/UserMapper.php +++ b/DataMapper/UserMapper.php @@ -16,6 +16,7 @@ namespace DesignPatterns; * entity types, dedicated mappers will handle one or a few. * (FROM http://en.wikipedia.org/wiki/Data_mapper_pattern) * + * * Examples: * - DB Object Relational Mapper (ORM) * @@ -23,15 +24,23 @@ namespace DesignPatterns; class UserMapper { - // the table where the mapper is mapping - protected $_dbTable; + protected $_adapter; public function __construct(array $options = null) { - // create new db connector on dbTable using specific table + /** + * create new Database connector on $_adapter using specific table + * + * $_adapter var could be a specific to a table class or a generic interface for + * connecting to Database and do certain jobs + */ } - // will save a certain user from memory to db + /** + * saves a user object from memory to Database + * + * @return boolean + */ public function save(User $user) { $data = array( @@ -42,33 +51,56 @@ class UserMapper if (null === ($id = $user->getUserId())) { unset($data['userid']); - $this->_dbTable->insert($data); + $this->_adapter->insert($data); + return true; } else { - $this->_dbTable->update($data, array('userid = ?' => $id)); + $this->_adapter->update($data, array('userid = ?' => $id)); + return true; } + + return false; } - // will find a user from db based on the id and return it to a User object in memory - public function find($id, User $user) + /** + * finds a user from Database based on ID and returns a User object located + * in memory + * + * @return User + */ + public function findById($id) { - $result = $this->_dbTable()->find($id); + $result = $this->_adapter->find($id); if (0 == count($result)) { return; } $row = $result->current(); - $user->setOptions($row); + var user = new User(); + $user->setUserID($row['userid']); + $user->setUsername($row['username']); + $user->setEmail($row['email']); + + return user; } - // will fetch all entries from a table to memory - public function fetchAll() + /** + * fetches an array from Database and returns an array of User objects + * located in memory + * + * @return array + */ + public function findAll() { - $resultSet = $this->_dbTable()->fetchAll(); + $resultSet = $this->_adapter->findAll(); $entries = array(); foreach ($resultSet as $row) { + $entry = new User(); - $entry->setOptions($row); + $user->setUserID($row['userid']); + $user->setUsername($row['username']); + $user->setEmail($row['email']); + $entries[] = $entry; } From a2c33a4c584a333197ff8faa03adfb2c96259363 Mon Sep 17 00:00:00 2001 From: kbariotis Date: Mon, 13 May 2013 20:48:37 +0300 Subject: [PATCH 3/5] some more code examples --- DataMapper/Test.php | 10 +++++++++- DataMapper/User.php | 8 ++++---- DataMapper/UserMapper.php | 6 ++++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/DataMapper/Test.php b/DataMapper/Test.php index 7aba20e..a81f3d7 100644 --- a/DataMapper/Test.php +++ b/DataMapper/Test.php @@ -10,10 +10,18 @@ namespace DesignPatterns; $userMapper = new UserMapper(); - $user = $userMapper->find(1); + // fetch a record from Database + $user = $userMapper->findById(1); if ($user !== null) { echo "Hello " . $user->getUsername() . ". Your email is " . $user->getEmail(); } + // save a new record on Database + var $newUser = new User('', 'Odysseus', 'Odysseus@ithaca.gr'); + $userMapper->save($newUser); + + // fetch all from a table on Database + var $user = $userMapper->findAll(); + ?> \ No newline at end of file diff --git a/DataMapper/User.php b/DataMapper/User.php index 8edfa69..dd19cbe 100644 --- a/DataMapper/User.php +++ b/DataMapper/User.php @@ -14,11 +14,11 @@ class User protected $username; protected $email; - public function __construct(array $options = null) + public function __construct($id, $username, $email) { - if (is_array($options)) { - $this->setOptions($options); - } + $this->id = $id; + $this->username = $username; + $this->email = $email; } public function getUserId() { diff --git a/DataMapper/UserMapper.php b/DataMapper/UserMapper.php index 0688564..1672800 100644 --- a/DataMapper/UserMapper.php +++ b/DataMapper/UserMapper.php @@ -31,8 +31,8 @@ class UserMapper /** * create new Database connector on $_adapter using specific table * - * $_adapter var could be a specific to a table class or a generic interface for - * connecting to Database and do certain jobs + * $_adapter var could be a specific to a table class or a generic + * interface for connecting to Database and do certain jobs */ } @@ -43,12 +43,14 @@ class UserMapper */ public function save(User $user) { + // $data keys shoulds correspond to valid Table columns on the Database $data = array( 'userid' => $user->getUserId(), 'username' => $user->getUsername(), 'email' => $user->getEmail(), ); + // if no ID specified create new user else update the one in the Database if (null === ($id = $user->getUserId())) { unset($data['userid']); $this->_adapter->insert($data); From 2ca016fb66b16ae1591baee64c7c5f1c1cf9986d Mon Sep 17 00:00:00 2001 From: kbariotis Date: Mon, 13 May 2013 20:50:32 +0300 Subject: [PATCH 4/5] some fixes on comments --- DataMapper/Test.php | 6 +++--- DataMapper/UserMapper.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/DataMapper/Test.php b/DataMapper/Test.php index a81f3d7..8500a85 100644 --- a/DataMapper/Test.php +++ b/DataMapper/Test.php @@ -10,18 +10,18 @@ namespace DesignPatterns; $userMapper = new UserMapper(); - // fetch a record from Database + /* fetch a record from Database */ $user = $userMapper->findById(1); if ($user !== null) { echo "Hello " . $user->getUsername() . ". Your email is " . $user->getEmail(); } - // save a new record on Database + /* save a new record on Database */ var $newUser = new User('', 'Odysseus', 'Odysseus@ithaca.gr'); $userMapper->save($newUser); - // fetch all from a table on Database + /* fetch all from a table on Database */ var $user = $userMapper->findAll(); ?> \ No newline at end of file diff --git a/DataMapper/UserMapper.php b/DataMapper/UserMapper.php index 1672800..ff5bcce 100644 --- a/DataMapper/UserMapper.php +++ b/DataMapper/UserMapper.php @@ -43,14 +43,14 @@ class UserMapper */ public function save(User $user) { - // $data keys shoulds correspond to valid Table columns on the Database + /* $data keys shoulds correspond to valid Table columns on the Database */ $data = array( 'userid' => $user->getUserId(), 'username' => $user->getUsername(), 'email' => $user->getEmail(), ); - // if no ID specified create new user else update the one in the Database + /* if no ID specified create new user else update the one in the Database */ if (null === ($id = $user->getUserId())) { unset($data['userid']); $this->_adapter->insert($data); From 9e26e7c864cf474c9fd6e10be6e2dd221e64cacc Mon Sep 17 00:00:00 2001 From: Konstantinos Bariotis Date: Wed, 15 May 2013 21:33:04 +0300 Subject: [PATCH 5/5] Update Test.php --- DataMapper/Test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DataMapper/Test.php b/DataMapper/Test.php index 8500a85..47e8c9d 100644 --- a/DataMapper/Test.php +++ b/DataMapper/Test.php @@ -22,6 +22,6 @@ namespace DesignPatterns; $userMapper->save($newUser); /* fetch all from a table on Database */ - var $user = $userMapper->findAll(); + var $usersArray = $userMapper->findAll(); -?> \ No newline at end of file +?>