Created
May 30, 2012 14:06
-
-
Save jaytaph/2836548 to your computer and use it in GitHub Desktop.
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
class Model_Blog_Entity extends Model_Entity { | |
protected $_id; | |
protected $_title; | |
protected $_post; | |
protected $_author; | |
public function getId() { | |
return $this->_id; } | |
public function setId() { | |
$this->_id = $id; | |
} | |
... | |
} |
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
class Model_Blog_Mapper extends Model_Mapper { | |
protected $_tableName = “blogposts”; | |
protected $_primaryKey = “id”; | |
protected function _toArray(Model_Entity $obj) { | |
$data = array(); | |
$data[‘id’] = $obj->getId(); | |
$data[‘title’] = $obj->getTitle(); | |
... | |
return $data; | |
} | |
protected function _fromArray(array $data) { | |
$obj = new Model_Blog_Entity(); | |
$obj->setId($data[‘id’]); | |
$obj->setTitle($data[‘title’]); | |
... | |
return $obj; | |
} | |
} |
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
abstract class Model_Entity { | |
} |
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
abstract class Model_Mapper { | |
protected $_tableName = ‘’; | |
protected $_primaryKey = ‘’; | |
protected $_table; | |
function __construct() { | |
$this->_table = new Zend_Db_Table($this->_tableName); | |
} | |
function findByPk($value) { | |
$select = $this->_table->select()->where($this->_primaryKey . ‘ = ?’, $value); | |
$row = $select->fetchRow($select); | |
if ($row == null) return null; | |
return $this->_fromArray($row->toArray()); | |
} | |
public function fromArray(array $data) { | |
return $this->_fromArray($data); | |
} | |
abstract protected function _toArray(Model_Entity $obj); | |
abstract protected function _fromArray(array $data); | |
function save(Model_Entity $obj) { | |
$data = $this->_toArray($obj); | |
if ($obj->getId() == 0) { | |
$row = null; | |
} else { | |
// Check if record exists on PK. | |
$select = $this->_table->select()->where($this->_primaryKey.' = ?' , $obj->getId()); | |
$row = $this->_table->fetchRow($select); | |
} | |
if ($row == null) { | |
// INSERT, record does not exists | |
$this->_table->insert($data); | |
} else { | |
// UPDATE, record does exists | |
$where = $this->_table->getAdapter()->quoteInto($this->_primaryKey.' = ?', $obj->getId()); | |
$this->_table->update($data, $where); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment