PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/templates/mapper.tpl

https://code.google.com/
Smarty Template | 409 lines | 361 code | 48 blank | 0 comment | 96 complexity | a3c5a2d9dad8983152dad7cc16476040 MD5 | raw file
  1. <?="<?php\n"?>
  2. /**
  3. * Application Model Mappers
  4. *
  5. * @package <?=$this->_namespace?>_Model
  6. * @subpackage Mapper
  7. * @author <?=$this->_author . "\n"?>
  8. * @copyright <?=$this->_copyright . "\n"?>
  9. * @license <?=$this->_license . "\n"?>
  10. */
  11. <?php if ($this->_addRequire):?>
  12. /**
  13. * Table definition for this class
  14. * @see <?=$this->_namespace?>_Model_DbTable_<?=$this->_className."\n"?>
  15. */
  16. require_once dirname(__FILE__) . '/../DbTable/<?=$this->_className?>.php';
  17. <?php endif ?>
  18. /**
  19. * Data Mapper implementation for <?=$this->_namespace?>_Model_<?=$this->_className."\n"?>
  20. *
  21. * @package <?=$this->_namespace?>_Model
  22. * @subpackage Mapper
  23. * @author <?=$this->_author . "\n"?>
  24. */
  25. class <?=$this->_namespace?>_Model_Mapper_<?=$this->_className?> extends <?=$this->_includeMapper->getParentClass() . "\n"?>
  26. {
  27. <?php $vars = $this->_includeMapper->getVars();
  28. if (! empty($vars)) {
  29. echo "\n$vars\n";
  30. }
  31. ?>
  32. /**
  33. * Returns an array, keys are the field names.
  34. *
  35. * @param <?=$this->_namespace?>_Model_<?=$this->_className?> $model
  36. * @return array
  37. */
  38. public function toArray($model)
  39. {
  40. if (! $model instanceof <?=$this->_namespace?>_Model_<?=$this->_className?>) {
  41. <?php if (! empty($this->_loggerName)):?>
  42. if (is_object($model)) {
  43. $message = get_class($model) . " is not a <?=$this->_namespace?>_Model_<?=$this->_className?> object in toArray for " . get_class($this);
  44. } else {
  45. $message = "$model is not a <?=$this->_namespace?>_Model_<?=$this->_className?> object in toArray for " . get_class($this);
  46. }
  47. $this->_logger->log($message, Zend_Log::ERR);
  48. <?php endif; ?>
  49. throw new Exception('Unable to create array: invalid model passed to mapper');
  50. }
  51. $result = array(<?php echo "\n";
  52. foreach ($this->_columns as $column):
  53. ?>
  54. '<?=$column['field']?>' => $model->get<?=$column['capital']?>(),
  55. <?php endforeach;?>
  56. );
  57. return $result;
  58. }
  59. /**
  60. * Returns the DbTable class associated with this mapper
  61. *
  62. * @return <?=$this->_namespace?>_Model_DbTable_<?=$this->_className . "\n"?>
  63. */
  64. public function getDbTable()
  65. {
  66. if ($this->_dbTable === null) {
  67. $this->setDbTable('<?=$this->_namespace?>_Model_DbTable_<?=$this->_className?>');
  68. }
  69. return $this->_dbTable;
  70. }
  71. /**
  72. * Deletes the current model
  73. *
  74. * @param <?=$this->_namespace?>_Model_<?=$this->_className?> $model The model to <?php if ($this->_softDeleteColumn != null): ?>mark as deleted
  75. <?php else: ?>delete
  76. <?php endif;?>
  77. <?php if ($this->_softDeleteColumn == null): ?>
  78. * @see <?=$this->_namespace?>_Model_DbTable_TableAbstract::delete()
  79. <?php endif;?>
  80. * @return int
  81. */
  82. public function delete($model)
  83. {
  84. if (! $model instanceof <?=$this->_namespace?>_Model_<?=$this->_className?>) {
  85. <?php if (! empty($this->_loggerName)):?>
  86. if (is_object($model)) {
  87. $message = get_class($model) . " is not a <?=$this->_namespace?>_Model_<?=$this->_className?> object in delete for " . get_class($this);
  88. } else {
  89. $message = "$model is not a <?=$this->_namespace?>_Model_<?=$this->_className?> object in delete for " . get_class($this);
  90. }
  91. $this->_logger->log($message, Zend_Log::ERR);
  92. <?php endif; ?>
  93. throw new Exception('Unable to delete: invalid model passed to mapper');
  94. }
  95. $this->getDbTable()->getAdapter()->beginTransaction();
  96. try {
  97. <?php if ($this->_softDeleteColumn != null):
  98. foreach ($this->_columns as $column):
  99. if ($column['field'] == $this->_softDeleteColumn) {?>
  100. $model->set<?=$column['capital']?>(<?php
  101. if ($column['phptype'] == 'boolean') {
  102. echo 'true';
  103. } elseif (preg_match('/date/', $column['type'])) {
  104. echo 'Zend_Date::now()';
  105. } else {
  106. echo '1';
  107. }
  108. break;
  109. }
  110. endforeach;?>);
  111. $result = $model->save();
  112. <?php else: ?>
  113. <?php if ($this->_primaryKey['phptype'] == 'array') : ?>
  114. $where = array();
  115. <?php foreach ($this->_primaryKey['fields'] as $key) : ?>
  116. $pk_val = $model->get<?=$key['capital']?>();
  117. if ($pk_val === null) {
  118. <?php if (! empty($this->_loggerName)):?>
  119. $this->_logger->log('The value for <?=$key['capital']?> cannot be null in delete for ' . get_class($this), Zend_Log::ERR);
  120. <?php endif; ?>
  121. throw new Exception('The value for <?=$key['capital']?> cannot be null');
  122. } else {
  123. $where[] = $this->getDbTable()->getAdapter()->quoteInto('<?=$key['field']?> = ?', $pk_val);
  124. }
  125. <?php endforeach; ?>
  126. <?php else :?>
  127. $where = $this->getDbTable()->getAdapter()->quoteInto('<?=$this->_primaryKey['field']?> = ?', $model->get<?=$this->_primaryKey['capital']?>());
  128. <?php endif; ?>
  129. $result = $this->getDbTable()->delete($where);
  130. $this->getDbTable()->getAdapter()->commit();
  131. } catch (Exception $e) {
  132. <?php if (! empty($this->_loggerName)):?>
  133. $message = 'Exception encountered while attempting to delete ' . get_class($this);
  134. if (! empty($where)) {
  135. $message .= ' Where: ';
  136. <?php if ($this->_primaryKey['phptype'] == 'array') : ?>
  137. foreach ($where as $where_clause) {
  138. $message .= $where_clause;
  139. }
  140. <?php else: ?>
  141. $message .= $where;
  142. <?php endif; ?>
  143. } else {
  144. $message .= ' with an empty where';
  145. }
  146. $message .= ' Exception: ' . $e->getMessage();
  147. $this->_logger->log($message, Zend_Log::ERR);
  148. $this->_logger->log($e->getTraceAsString(), Zend_Log::DEBUG);
  149. <?php endif; ?>
  150. $this->getDbTable()->getAdapter()->rollback();
  151. $result = false;
  152. }
  153. return $result;
  154. <?php endif; ?>
  155. }
  156. /**
  157. * Saves current row, and optionally dependent rows
  158. *
  159. * @param <?=$this->_namespace?>_Model_<?=$this->_className?> $model
  160. * @param boolean $ignoreEmptyValues Should empty values saved
  161. * @param boolean $recursive Should the object graph be walked for all related elements
  162. * @param boolean $useTransaction Flag to indicate if save should be done inside a database transaction
  163. * @return boolean If the save action was successful
  164. */
  165. public function save(<?=$this->_namespace?>_Model_<?=$this->_className?> $model,
  166. $ignoreEmptyValues = true, $recursive = false, $useTransaction = true
  167. ) {
  168. $data = $model->toArray();
  169. if ($ignoreEmptyValues) {
  170. foreach ($data as $key => $value) {
  171. if ($value === null or $value === '') {
  172. unset($data[$key]);
  173. }
  174. }
  175. }
  176. <?php if ($this->_primaryKey['phptype'] == 'array') : ?>
  177. $primary_key = array();
  178. <?php foreach ($this->_primaryKey['fields'] as $key) : ?>
  179. $pk_val = $model->get<?=$key['capital']?>();
  180. if ($pk_val === null) {
  181. <?php if (! empty($this->_loggerName)):?>
  182. $this->_logger->log('The value for <?=$key['capital']?> cannot be null in save for ' . get_class($this), Zend_Log::ERR);
  183. <?php endif; ?>
  184. return false;
  185. } else {
  186. $primary_key['<?=$key['field']?>'] = $pk_val;
  187. }
  188. <?php endforeach; ?>
  189. $exists = $this->find($primary_key, null);
  190. $success = true;
  191. if ($useTransaction) {
  192. $this->getDbTable()->getAdapter()->beginTransaction();
  193. }
  194. try {
  195. // Check for current existence to know if needs to be inserted
  196. if ($exists === null) {
  197. $this->getDbTable()->insert($data);
  198. <?php else :?>
  199. $primary_key = $model->get<?=$this->_primaryKey['capital']?>();
  200. $success = true;
  201. if ($useTransaction) {
  202. $this->getDbTable()->getAdapter()->beginTransaction();
  203. }
  204. <?php if (! $this->_primaryKey['foreign_key']): ?>
  205. unset($data['<?=$this->_primaryKey['field']?>']);
  206. try {
  207. if ($primary_key === null) {
  208. <?php else: ?>
  209. $exists = $this->find($primary_key, null);
  210. try {
  211. if ($exists === null) {
  212. <?php endif; ?>
  213. $primary_key = $this->getDbTable()->insert($data);
  214. if ($primary_key) {
  215. $model->set<?=$this->_primaryKey['capital']?>($primary_key);
  216. } else {
  217. $success = false;
  218. }
  219. <?php endif;?>
  220. } else {
  221. $this->getDbTable()
  222. ->update($data,
  223. array(<?php echo "\n ";
  224. if ($this->_primaryKey['phptype'] == 'array') {
  225. $fields = count($this->_primaryKey['fields']);
  226. $i = 0;
  227. foreach ($this->_primaryKey['fields'] as $key) {
  228. echo '\'' . $key['field'] . ' = ?\' => $primary_key[\'' . $key['field'] . '\']';
  229. $i++;
  230. if ($i != $fields) {
  231. echo ",\n ";
  232. }
  233. }
  234. } else {
  235. echo '\'' . $this->_primaryKey['field'] . ' = ?\' => $primary_key';
  236. }
  237. echo "\n";?>
  238. )
  239. );
  240. }
  241. <?php if (count($this->getDependentTables()) > 0) :?>
  242. if ($recursive) {
  243. <?php foreach ($this->getDependentTables() as $key) : ?>
  244. if ($success && $model->get<?=$this->_getRelationName($key, 'dependent')?>(false) !== null) {
  245. <?php if ($key['type'] !== 'many') : ?>
  246. $success = $success &&
  247. $model->get<?=$this->_getRelationName($key, 'dependent')?>()
  248. <?php if ($this->_primaryKey['phptype'] !== 'array') : ?>
  249. ->set<?=$this->_getCapital($key['column_name'])?>($primary_key)
  250. <?php endif;?>
  251. ->save($ignoreEmptyValues, $recursive, false);
  252. <?php else: ?>
  253. $<?=$this->_getClassName($key['foreign_tbl_name'])?> = $model->get<?=$this->_getRelationName($key, 'dependent')?>();
  254. foreach ($<?=$this->_getClassName($key['foreign_tbl_name'])?> as $value) {
  255. $success = $success &&
  256. $value<?php if ($this->_primaryKey['phptype'] !== 'array') : ?>
  257. ->set<?=$this->_getCapital($key['column_name'])?>($primary_key)
  258. <?php elseif (is_array($key['column_name'])) :
  259. foreach ($key['column_name'] as $column) : ?>
  260. ->set<?=$this->_getCapital($column)?>($primary_key['<?php echo $column ?>'])
  261. <?php endforeach; ?>
  262. <?php endif;?>
  263. ->save($ignoreEmptyValues, $recursive, false);
  264. if (! $success) {
  265. break;
  266. }
  267. }
  268. <?php endif; ?>
  269. }
  270. <?php endforeach; ?>
  271. }
  272. <?php endif; ?>
  273. if ($useTransaction && $success) {
  274. $this->getDbTable()->getAdapter()->commit();
  275. } elseif ($useTransaction) {
  276. $this->getDbTable()->getAdapter()->rollback();
  277. }
  278. <?php if (! empty($this->_loggerName)):?>
  279. if (! $success) {
  280. $message = 'Unable to save ' . get_class($this) . ' and all dependents';
  281. $this->_logger->log($message, Zend_Log::WARN);
  282. }
  283. <?php endif; ?>
  284. } catch (Exception $e) {
  285. <?php if (! empty($this->_loggerName)):?>
  286. $message = 'Exception encountered while attempting to save ' . get_class($this);
  287. if (! empty($primary_key)) {
  288. <?php if ($this->_primaryKey['phptype'] == 'array') : ?>
  289. $message .= ' id:';
  290. <?php foreach ($this->_primaryKey['fields'] as $key) : ?>
  291. $message .= ' <?=$key['field']?> => ' . $primary_key['<?=$key['field']?>'];
  292. <?php endforeach; ?>
  293. <?php else: ?>
  294. $message .= ' id: ' . $primary_key;
  295. <?php endif; ?>
  296. } else {
  297. $message .= ' with an empty primary key ';
  298. }
  299. $message .= ' Exception: ' . $e->getMessage();
  300. $this->_logger->log($message, Zend_Log::ERR);
  301. $this->_logger->log($e->getTraceAsString(), Zend_Log::DEBUG);
  302. <?php endif; ?>
  303. if ($useTransaction) {
  304. $this->getDbTable()->getAdapter()->rollback();
  305. }
  306. $success = false;
  307. }
  308. return $success;
  309. }
  310. /**
  311. * Finds row by primary key
  312. *
  313. * @param <?=$this->_primaryKey['phptype']?> $primary_key
  314. * @param <?=$this->_namespace?>_Model_<?=$this->_className?>|null $model
  315. * @return <?=$this->_namespace?>_Model_<?=$this->_className?>|null The object provided or null if not found
  316. */
  317. public function find($primary_key, $model)
  318. {
  319. $result = $this->getRowset($primary_key);
  320. if (is_null($result)) {
  321. return null;
  322. }
  323. $row = $result->current();
  324. $model = $this->loadModel($row, $model);
  325. return $model;
  326. }
  327. /**
  328. * Loads the model specific data into the model object
  329. *
  330. * @param Zend_Db_Table_Row_Abstract|array $data The data as returned from a Zend_Db query
  331. * @param <?=$this->_namespace?>_Model_<?=$this->_className?>|null $entry The object to load the data into, or null to have one created
  332. * @return <?=$this->_namespace?>_Model_<?=$this->_className?> The model with the data provided
  333. */
  334. public function loadModel($data, $entry)
  335. {
  336. if ($entry === null) {
  337. $entry = new <?=$this->_namespace?>_Model_<?=$this->_className?>();
  338. }
  339. if (is_array($data)) {
  340. $entry<?php
  341. $count = count($this->_columns);
  342. foreach ($this->_columns as $column):
  343. $count--;
  344. ?>->set<?=$column['capital']?>($data['<?=$column['field']?>'])<?if ($count> 0) echo "\n ";
  345. endforeach; ?>;
  346. } elseif ($data instanceof Zend_Db_Table_Row_Abstract || $data instanceof stdClass) {
  347. $entry<?php
  348. $count = count($this->_columns);
  349. foreach ($this->_columns as $column):
  350. $count--;
  351. ?>->set<?=$column['capital']?>($data-><?=$column['field']?>)<?if ($count> 0) echo "\n ";
  352. endforeach; ?>;
  353. }
  354. $entry->setMapper($this);
  355. return $entry;
  356. }
  357. <?php $functions = $this->_includeMapper->getFunctions();
  358. if (! empty($functions)) {
  359. echo "\n$functions\n";
  360. } ?>
  361. }