Created
October 20, 2011 23:15
-
-
Save fideloper/1302686 to your computer and use it in GitHub Desktop.
Zend Data Mapping example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Abstract "model" class | |
class App_Model_Abstract | |
{ | |
public function __construct(array $options = null) | |
{ | |
if (is_array($options)) { | |
$this->setOptions($options); | |
} | |
} | |
public function __set($name, $value) | |
{ | |
$method = 'set' . $name; | |
if (('mapper' == $name) || !method_exists($this, $method)) { | |
throw new Exception('Invalid note property'); | |
} | |
$this->$method($value); | |
} | |
public function __get($name) | |
{ | |
$method = 'get' . $name; | |
if (('mapper' == $name) || !method_exists($this, $method)) { | |
throw new Exception('Invalid note 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; | |
} | |
} | |
//Table class | |
class App_Model_Db_User extends Zend_Db_Table_Abstract | |
{ | |
/** Table name */ | |
protected $_name = 'users'; | |
protected $_primary = 'id'; | |
} | |
//User model class - The class most interfaced with in application code | |
class App_Model_User extends App_Model_Abstract { | |
protected $_id; | |
protected $_email; | |
protected $_date_added; | |
protected $_active; | |
protected $_user_type; | |
protected $_mapper; | |
protected $_has_company; | |
protected $_companies = array(); | |
const USER_TYPE_USER = 'user'; | |
const USER_TYPE_ADMIN = 'admin'; | |
/* | |
GETTERS / SETTERS | |
*/ | |
public function setId($id) { | |
$this->_id = $id; | |
return $this; | |
} | |
public function getId() { | |
return $this->_id; | |
} | |
public function setEmail($email) { | |
$this->_email = substr($email, 0, 120); | |
return $this; | |
} | |
public function getEmail() { | |
return $this->_email; | |
} | |
public function setDateAdded($last) { | |
$this->_date_added = $last; | |
return $this; | |
} | |
public function getDateAdded() { | |
return $this->_date_added; | |
} | |
public function setActive($active) { | |
if($active) { | |
$this->_active = 1; | |
} else { | |
$this->_active = 0; | |
} | |
return $this; | |
} | |
public function getActive() { | |
return $this->_active; | |
} | |
public function setUserType($type) { | |
$this->_user_type = substr($type, 0, 10); | |
return $this; | |
} | |
public function getUserType() { | |
return $this->_user_type; | |
} | |
public function setMapper($mapper) { | |
$this->_mapper = $mapper; | |
return $this; | |
} | |
public function getMapper() { | |
if (null === $this->_mapper) { | |
$this->setMapper(new App_Model_Mappers_User()); | |
} | |
return $this->_mapper; | |
} | |
/* | |
BASIC DATA | |
*/ | |
public function save() { | |
$id = $this->getMapper()->save($this); | |
$this->setFbId($id); | |
return $id; | |
} | |
public function update() { | |
return $this->getMapper()->update($this); | |
} | |
public function find($id) { | |
if(!$this->getMapper()->find($id, $this)) { | |
return false; | |
} | |
return $this; | |
} | |
} | |
//Data mapper (does heavy lifting of this example) | |
class App_Model_Mappers_User { | |
protected $_dbTable; | |
const TABLE = 'App_Model_Db_User'; | |
public function setDbTable($dbTable) { | |
if (is_string($dbTable)) { | |
$dbTable = new $dbTable(); | |
} | |
if (!$dbTable instanceof Zend_Db_Table_Abstract) { | |
throw new Exception('Invalid table data gateway provided'); | |
} | |
$this->_dbTable = $dbTable; | |
return $this; | |
} | |
public function getDbTable() { | |
if (null === $this->_dbTable) { | |
$this->setDbTable(self::TABLE); | |
} | |
return $this->_dbTable; | |
} | |
public function save(App_Model_User $obj) { | |
$data = array( | |
'id' => $obj->getId(), | |
'email' => $obj->getEmail(), | |
'date_added' => $obj->getDateAdded(), | |
'active' => $obj->getActive(), | |
'user_type' => $obj->getUserType() | |
); | |
return $this->getDbTable()->insert($data); | |
} | |
public function update(App_Model_User $obj) { | |
$data = array( | |
'id' => $obj->getId(), | |
'email' => $obj->getEmail(), | |
'date_added' => $obj->getDateAdded(), | |
'active' => $obj->getActive(), | |
'user_type' => $obj->getUserType() | |
); | |
$where = $this->getDbTable()->getAdapter()->quoteInto('`id` = ?', $obj->getId()); | |
return $this->getDbTable()->update($data, $where); | |
} | |
public function find($id, App_Model_User $obj) { | |
$result = $this->getDbTable()->find($id); | |
if (0 == count($result)) { | |
return false; | |
} | |
$row = $result->current(); | |
$obj->setId($row->id) | |
->setEmail($row->email) | |
->setDateAdded($row->date_added) | |
->setActive($row->active) | |
->setUserType($row->user_type); | |
return $obj; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment