Created
March 13, 2012 14:56
-
-
Save nlenepveu/2029272 to your computer and use it in GitHub Desktop.
Model abstraction - class App_Model_AbstractMapper
This file contains hidden or 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 class App_Model_AbstractMapper | |
{ | |
/** | |
* @var Zend_Db_Table_Abstract | |
*/ | |
private $_dbTable; | |
/** | |
* @var App_Model_Abstract | |
*/ | |
private $_modelPrototype; | |
/** | |
* Public constructor | |
* | |
* @param Zend_Db_Table_Abstract $dbTable | |
* @param App_Model_Abstract $dbModelPrototype | |
*/ | |
public function __construct(Zend_Db_Table_Abstract $dbTable = null, App_Model_Abstract $modelPrototype = null) | |
{ | |
$this->setDbTable($dbTable ?: $this->_getDefaultDbTable()); | |
$this->setModelPrototype($modelPrototype ?: $this->_getDefaultModel()); | |
} | |
/** | |
* Set the DbTable instance | |
* | |
* @param Zend_Db_Table_Abstract $dbTable | |
* | |
* @return App_Model_AbstractMapper | |
*/ | |
public function setDbTable(Zend_Db_Table_Abstract $dbTable) | |
{ | |
$this->_dbTable = $dbTable; | |
return $this; | |
} | |
/** | |
* Get the associated DbTable instance | |
* | |
* @return Zend_Db_Table_Abstract | |
*/ | |
public function getDbTable() | |
{ | |
return $this->_dbTable; | |
} | |
/** | |
* Set the prototype of the model | |
* | |
* @param App_Model_Abstract $modelPrototype | |
* | |
* @return App_Model_AbstractMapper | |
*/ | |
public function setModelPrototype(App_Model_Abstract $modelPrototype) | |
{ | |
$this->_modelPrototype = $modelPrototype; | |
return $this; | |
} | |
/** | |
* Fetch data and return a models collection | |
* | |
* @param string|array|Zend_Db_Table_Select $where OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object. | |
* @param string|array $order OPTIONAL An SQL ORDER clause. | |
* @param int $count OPTIONAL An SQL LIMIT count. | |
* @param int $offset OPTIONAL An SQL LIMIT offset. | |
* | |
* @throws Exception | |
* | |
* @return array | |
*/ | |
public function fetchAll($where = null, $order = null, $count = null, $offset = null) | |
{ | |
$rowset = $this->getDbTable()->fetchAll($where, $order, $count, $offset); | |
$models = array(); | |
foreach ($rowset as $row) { | |
$models[] = $model = clone $this->_modelPrototype; | |
$model->initialize($row); | |
} | |
return $models; | |
} | |
/** | |
* Insert a model | |
* | |
* @param App_Model_Abstract $model | |
* | |
* @return mixed | |
*/ | |
public function insert(App_Model_Abstract $model) | |
{ | |
$id = $this->getDbTable()->insert(get_object_vars($model)); | |
$primary = $this->_getPrimary(); | |
$model->{$primary} = $id; | |
return $id; | |
} | |
/** | |
* Update a model | |
* . | |
* @param App_Model_Abstract $model | |
* | |
* @return int | |
*/ | |
public function update(App_Model_Abstract $model) | |
{ | |
$primary = $this->_getPrimary(); | |
$id = $model->{$primary}; | |
if (!isset($id)) { | |
throw new Exception('No key set, could not be updated'); | |
} | |
return $this->getDbTable()->update(get_object_vars($model), array("$primary = ?" => $id)); | |
} | |
/** | |
* Delete a model | |
* | |
* @param mixed $id | |
* | |
* @return int | |
*/ | |
public function delete($id) | |
{ | |
$primary = $this->_getPrimary(); | |
return $this->getDbTable()->delete(array("$primary = ?" => $id)); | |
} | |
/** | |
* Load a model | |
* | |
* @return App_Model_Abstract $model | |
*/ | |
function load($id) | |
{ | |
$result = $this->getDbTable()->find($id); | |
if (0 == count($result)) { | |
return; | |
} | |
$model = clone $this->_modelPrototype; | |
$model->initialize($result->current()); | |
return $model; | |
} | |
/** | |
* Return the default linked DbTable | |
* | |
* @return Zend_Db_Table_Abstract | |
*/ | |
protected function _getDefaultDbTable() | |
{ | |
$className = str_replace(array('Model_', 'Mapper'), array('Model_DbTable_', ''), get_class($this)); | |
if (! class_exists($className, true)) { | |
throw new Exception(sprintf("No default DbTable defined for the mapper %s, %s expected", get_class($this), $className)); | |
} | |
return new $className; | |
} | |
/** | |
* Return the default linked Model | |
* | |
* @return App_Model_Abstract | |
*/ | |
protected function _getDefaultModel() | |
{ | |
$className = str_replace('Mapper', '', get_class($this)); | |
if (! class_exists($className, true)) { | |
throw new Exception(sprintf("No default model defined for the mapper %s, %s expected", get_class($this), $className)); | |
} | |
return new $className(null, $this); | |
} | |
/** | |
* Return the primary key | |
* | |
* @return string | |
*/ | |
protected function _getPrimary() | |
{ | |
return current($this->getDbTable()->info(Zend_Db_Table_Abstract::PRIMARY)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment