added DataMapper Example

This commit is contained in:
kbariotis
2013-05-13 20:40:21 +03:00
parent 4ff0def140
commit 73cd3bf9cd
3 changed files with 73 additions and 47 deletions

View File

@@ -0,0 +1,19 @@
<?php
namespace DesignPatterns;
/**
* DataMapper pattern
*
* Testing the DataMapper Pattern
*/
$userMapper = new UserMapper();
$user = $userMapper->find(1);
if ($user !== null) {
echo "Hello " . $user->getUsername() . ". Your email is " . $user->getEmail();
}
?>

View File

@@ -1,5 +1,13 @@
<?php
namespace DesignPatterns;
/**
* DataMapper pattern
*
* This is our representation of a DataBase record in the memory
*
*/
class User
{
protected $userId;
@@ -13,39 +21,6 @@ class User
}
}
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;
}

View File

@@ -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;
}