|
| 1 | +<?php |
| 2 | + |
| 3 | +class Application_Model_CommentMapper |
| 4 | +{ |
| 5 | + protected $_dbTable; |
| 6 | + |
| 7 | + public function setDbTable($dbTable) |
| 8 | + { |
| 9 | + if (is_string($dbTable)) { |
| 10 | + $dbTable = new $dbTable; |
| 11 | + } |
| 12 | + if (!$dbTable instanceof Zend_Db_Table_Abstract) { |
| 13 | + throw new Zend_Exception('Invalid data gateway provided'); |
| 14 | + } |
| 15 | + $this->_dbTable = $dbTable; |
| 16 | + return $this; |
| 17 | + } |
| 18 | + public function getDbTable() |
| 19 | + { |
| 20 | + if (null === $this->_dbTable) { |
| 21 | + $this->setDbTable('Application_Model_DbTable_Comment'); |
| 22 | + } |
| 23 | + return $this->_dbTable; |
| 24 | + } |
| 25 | + public function find($id, Application_Model_Comment $comment) |
| 26 | + { |
| 27 | + $resultSet = $this->getDbTable()->find($id); |
| 28 | + if (!empty ($resultSet)) { |
| 29 | + $comment->populate($resultSet->current()); |
| 30 | + } |
| 31 | + return $this; |
| 32 | + } |
| 33 | + public function fetchRow(Application_Model_Comment $comment, $where = null, $order = null) |
| 34 | + { |
| 35 | + $row = $this->getDbTable()->fetchRow($where, $order); |
| 36 | + if (!empty ($row)) { |
| 37 | + $comment->populate($row); |
| 38 | + } |
| 39 | + return $this; |
| 40 | + } |
| 41 | + public function fetchAll($where = null, $order = null, $count = null, $offset = null) |
| 42 | + { |
| 43 | + $entries = array (); |
| 44 | + $resultSet = $this->getDbTable()->fetchAll($where, $order, $count, $offset); |
| 45 | + foreach ($resultSet as $row) { |
| 46 | + $entries[] = new Application_Model_Comment($row); |
| 47 | + } |
| 48 | + return $entries; |
| 49 | + } |
| 50 | + public function save(Application_Model_Comment $comment) |
| 51 | + { |
| 52 | + $data = $comment->toArray(); |
| 53 | + unset ($data['id']); |
| 54 | + if (0 < $comment->getId()) { |
| 55 | + $this->getDbTable()->update($data, array ('id = ?' => $comment->getId())); |
| 56 | + } else { |
| 57 | + $this->getDbTable()->insert($data); |
| 58 | + } |
| 59 | + } |
| 60 | + public function delete(Application_Model_Comment $comment) |
| 61 | + { |
| 62 | + $this->getDbTable()->delete(array ('id = ?' => $comment->getId())); |
| 63 | + return $this; |
| 64 | + } |
| 65 | +} |
| 66 | + |
0 commit comments