Zend Data Mapping example


/ Published in: PHP
Save to your folder(s)

Example set of files for data mapping objects to db records. (Not perfect, but funcional)


Copy this code and paste it in your HTML
  1. <?php
  2. //Abstract "model" class
  3. class App_Model_Abstract
  4. {
  5.  
  6. public function __construct(array $options = null)
  7. {
  8. if (is_array($options)) {
  9. $this->setOptions($options);
  10. }
  11. }
  12.  
  13. public function __set($name, $value)
  14. {
  15. $method = 'set' . $name;
  16. if (('mapper' == $name) || !method_exists($this, $method)) {
  17. throw new Exception('Invalid note property');
  18. }
  19. $this->$method($value);
  20. }
  21.  
  22. public function __get($name)
  23. {
  24. $method = 'get' . $name;
  25. if (('mapper' == $name) || !method_exists($this, $method)) {
  26. throw new Exception('Invalid note property');
  27. }
  28. return $this->$method();
  29. }
  30.  
  31. public function setOptions(array $options)
  32. {
  33. $methods = get_class_methods($this);
  34. foreach ($options as $key => $value) {
  35. $method = 'set' . ucfirst($key);
  36. if (in_array($method, $methods)) {
  37. $this->$method($value);
  38. }
  39. }
  40. return $this;
  41. }
  42. }
  43.  
  44. //Table class
  45. class App_Model_Db_User extends Zend_Db_Table_Abstract
  46. {
  47. /** Table name */
  48. protected $_name = 'users';
  49. protected $_primary = 'id';
  50. }
  51.  
  52. //User model class - The class most interfaced with in application code
  53. class App_Model_User extends App_Model_Abstract {
  54.  
  55. protected $_id;
  56. protected $_email;
  57. protected $_date_added;
  58. protected $_active;
  59. protected $_user_type;
  60.  
  61. protected $_mapper;
  62.  
  63. protected $_has_company;
  64. protected $_companies = array();
  65.  
  66. const USER_TYPE_USER = 'user';
  67. const USER_TYPE_ADMIN = 'admin';
  68.  
  69. /*
  70. GETTERS / SETTERS
  71. */
  72.  
  73. public function setId($id) {
  74. $this->_id = $id;
  75. return $this;
  76. }
  77.  
  78. public function getId() {
  79. return $this->_id;
  80. }
  81.  
  82. public function setEmail($email) {
  83. $this->_email = substr($email, 0, 120);
  84. return $this;
  85. }
  86.  
  87. public function getEmail() {
  88. return $this->_email;
  89. }
  90.  
  91. public function setDateAdded($last) {
  92. $this->_date_added = $last;
  93. return $this;
  94. }
  95.  
  96. public function getDateAdded() {
  97. return $this->_date_added;
  98. }
  99.  
  100. public function setActive($active) {
  101. if($active) {
  102. $this->_active = 1;
  103. } else {
  104. $this->_active = 0;
  105. }
  106. return $this;
  107. }
  108.  
  109. public function getActive() {
  110. return $this->_active;
  111. }
  112.  
  113. public function setUserType($type) {
  114. $this->_user_type = substr($type, 0, 10);
  115. return $this;
  116. }
  117.  
  118. public function getUserType() {
  119. return $this->_user_type;
  120. }
  121.  
  122.  
  123. public function setMapper($mapper) {
  124. $this->_mapper = $mapper;
  125. return $this;
  126. }
  127.  
  128. public function getMapper() {
  129. if (null === $this->_mapper) {
  130. $this->setMapper(new App_Model_Mappers_User());
  131. }
  132. return $this->_mapper;
  133. }
  134.  
  135. /*
  136.   BASIC DATA
  137.   */
  138. public function save() {
  139. $id = $this->getMapper()->save($this);
  140. $this->setFbId($id);
  141. return $id;
  142. }
  143.  
  144. public function update() {
  145. return $this->getMapper()->update($this);
  146. }
  147.  
  148. public function find($id) {
  149. if(!$this->getMapper()->find($id, $this)) {
  150. return false;
  151. }
  152. return $this;
  153. }
  154. }
  155.  
  156. //Data mapper (does heavy lifting of this example)
  157. class App_Model_Mappers_User {
  158.  
  159. protected $_dbTable;
  160. const TABLE = 'App_Model_Db_User';
  161.  
  162. public function setDbTable($dbTable) {
  163. if (is_string($dbTable)) {
  164. $dbTable = new $dbTable();
  165. }
  166. if (!$dbTable instanceof Zend_Db_Table_Abstract) {
  167. throw new Exception('Invalid table data gateway provided');
  168. }
  169. $this->_dbTable = $dbTable;
  170. return $this;
  171. }
  172.  
  173. public function getDbTable() {
  174. if (null === $this->_dbTable) {
  175. $this->setDbTable(self::TABLE);
  176. }
  177. return $this->_dbTable;
  178. }
  179.  
  180. public function save(App_Model_User $obj) {
  181. $data = array(
  182. 'id' => $obj->getId(),
  183. 'email' => $obj->getEmail(),
  184. 'date_added' => $obj->getDateAdded(),
  185. 'active' => $obj->getActive(),
  186. 'user_type' => $obj->getUserType()
  187. );
  188.  
  189. return $this->getDbTable()->insert($data);
  190. }
  191.  
  192. public function update(App_Model_User $obj) {
  193. $data = array(
  194. 'id' => $obj->getId(),
  195. 'email' => $obj->getEmail(),
  196. 'date_added' => $obj->getDateAdded(),
  197. 'active' => $obj->getActive(),
  198. 'user_type' => $obj->getUserType()
  199. );
  200. $where = $this->getDbTable()->getAdapter()->quoteInto('`id` = ?', $obj->getId());
  201. return $this->getDbTable()->update($data, $where);
  202. }
  203.  
  204. public function find($id, App_Model_User $obj) {
  205. $result = $this->getDbTable()->find($id);
  206. if (0 == count($result)) {
  207. return false;
  208. }
  209. $row = $result->current();
  210. $obj->setId($row->id)
  211. ->setEmail($row->email)
  212. ->setDateAdded($row->date_added)
  213. ->setActive($row->active)
  214. ->setUserType($row->user_type);
  215.  
  216. return $obj;
  217. }
  218. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.