/ Published in: PHP
Example set of files for data mapping objects to db records. (Not perfect, but funcional)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php //Abstract "model" class class App_Model_Abstract { { $this->setOptions($options); } } public function __set($name, $value) { $method = 'set' . $name; throw new Exception('Invalid note property'); } $this->$method($value); } public function __get($name) { $method = 'get' . $name; throw new Exception('Invalid note property'); } return $this->$method(); } { foreach ($options as $key => $value) { $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; 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) { 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) { 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) { $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) { '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) { '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); return false; } $obj->setId($row->id) ->setEmail($row->email) ->setDateAdded($row->date_added) ->setActive($row->active) ->setUserType($row->user_type); return $obj; } }