mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-03 21:47:25 +02:00
added DataMapper Example
This commit is contained in:
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@@ -1,5 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DataMapper pattern
|
||||||
|
*
|
||||||
|
* This is our representation of a DataBase record in the memory
|
||||||
|
*
|
||||||
|
*/
|
||||||
class User
|
class User
|
||||||
{
|
{
|
||||||
protected $userId;
|
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() {
|
public function getUserId() {
|
||||||
return $this->userId;
|
return $this->userId;
|
||||||
}
|
}
|
||||||
|
@@ -16,6 +16,7 @@ namespace DesignPatterns;
|
|||||||
* entity types, dedicated mappers will handle one or a few.
|
* entity types, dedicated mappers will handle one or a few.
|
||||||
* (FROM http://en.wikipedia.org/wiki/Data_mapper_pattern)
|
* (FROM http://en.wikipedia.org/wiki/Data_mapper_pattern)
|
||||||
*
|
*
|
||||||
|
*
|
||||||
* Examples:
|
* Examples:
|
||||||
* - DB Object Relational Mapper (ORM)
|
* - DB Object Relational Mapper (ORM)
|
||||||
*
|
*
|
||||||
@@ -23,15 +24,23 @@ namespace DesignPatterns;
|
|||||||
class UserMapper
|
class UserMapper
|
||||||
{
|
{
|
||||||
|
|
||||||
// the table where the mapper is mapping
|
protected $_adapter;
|
||||||
protected $_dbTable;
|
|
||||||
|
|
||||||
public function __construct(array $options = null)
|
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)
|
public function save(User $user)
|
||||||
{
|
{
|
||||||
$data = array(
|
$data = array(
|
||||||
@@ -42,33 +51,56 @@ class UserMapper
|
|||||||
|
|
||||||
if (null === ($id = $user->getUserId())) {
|
if (null === ($id = $user->getUserId())) {
|
||||||
unset($data['userid']);
|
unset($data['userid']);
|
||||||
$this->_dbTable->insert($data);
|
$this->_adapter->insert($data);
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
$this->_dbTable->update($data, array('userid = ?' => $id));
|
$this->_adapter->update($data, array('userid = ?' => $id));
|
||||||
}
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// will find a user from db based on the id and return it to a User object in memory
|
return false;
|
||||||
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)) {
|
if (0 == count($result)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$row = $result->current();
|
$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();
|
$entries = array();
|
||||||
|
|
||||||
foreach ($resultSet as $row) {
|
foreach ($resultSet as $row) {
|
||||||
|
|
||||||
$entry = new User();
|
$entry = new User();
|
||||||
$entry->setOptions($row);
|
$user->setUserID($row['userid']);
|
||||||
|
$user->setUsername($row['username']);
|
||||||
|
$user->setEmail($row['email']);
|
||||||
|
|
||||||
$entries[] = $entry;
|
$entries[] = $entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user