/My/Scripts/DbMapper.php

https://github.com/mingomax/Zend-Db-Model-Creation · PHP · 377 lines · 343 code · 30 blank · 4 comment · 6 complexity · 56eb262c4dd9cad320ae4838ccd942a9 MD5 · raw file

  1. <?php
  2. class My_Scripts_DbMapper extends My_Scripts_ModelCreation
  3. {
  4. protected $_path;
  5. protected $_tableNameOriginal;
  6. protected $_tableName;
  7. protected $_populateSetters;
  8. protected $_primaryKey;
  9. protected $_insertSaveArray;
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. $this->_path = APPLICATION_PATH . '/models/mappers';
  14. $this->setupDbMapperFolder();
  15. $this->generateMapper();
  16. }
  17. protected function setupDbMapperFolder()
  18. {
  19. if (!is_dir($this->_path)) {
  20. mkdir($this->_path, 0755);
  21. $this->_logger->info('Mappers Folder Created <br />');
  22. return;
  23. }
  24. return;
  25. }
  26. protected function generateMapper()
  27. {
  28. //get the tables in the database
  29. $dbTables = $this->getDbTables();
  30. //get the describe of the table
  31. foreach ($dbTables as $table) {
  32. $describeTable = $this->describeTable($table);
  33. if ($describeTable) {
  34. $this->_tableNameOriginal = $table;
  35. $this->_tableName = Zend_Filter::filterStatic($table, 'StringToLower');
  36. $this->_tableName = Zend_Filter::filterStatic($this->_tableName, 'Word_UnderscoreToCamelCase');
  37. $this->_primaryKey = $this->getPrimaryKey($describeTable);
  38. $fields = array();
  39. foreach ($describeTable as $tableMeta)
  40. {
  41. $fields[] = array(
  42. 'Name' => (string) $this->fixColumnName($tableMeta['COLUMN_NAME'], true),
  43. 'Database_Column_Name' => $tableMeta['COLUMN_NAME'],
  44. );
  45. }
  46. $this->generatePopulateSetters($fields);
  47. $this->generateInsertSaveArray($fields);
  48. $code = $this->createMapper();
  49. $this->saveMapperFile($code);
  50. }
  51. }
  52. }
  53. public function dbTableMethods()
  54. {
  55. //setDbTable
  56. $methods[] = array(
  57. 'name' => 'setDbTable',
  58. 'parameters' => array(
  59. array(
  60. 'name' => 'dbTable',
  61. ),
  62. ),
  63. 'body' => 'if (is_string($dbTable)) {' . "\n " .
  64. '$dbTable = new $dbTable();' . "\n" .
  65. '}' . "\n" .
  66. 'if (!$dbTable instanceof Zend_Db_Table_Abstract) {' . "\n " .
  67. 'throw new Exception(\'Invalid table data gateway provided\');' . "\n" .
  68. '}' . "\n" .
  69. '$this->_dbTable = $dbTable;' . "\n" .
  70. 'return $this;',
  71. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  72. 'shortDescription' => 'setDbTable',
  73. 'tags' => array(
  74. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  75. 'paramName' => 'dbTable',
  76. 'datatype' => 'object',
  77. )),
  78. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  79. 'datatype' => 'object',
  80. )),
  81. ),
  82. )),
  83. );
  84. //getDbTable
  85. $methods[] = array(
  86. 'name' => 'getDbTable',
  87. 'body' => 'if (null === $this->_dbTable) {' . "\n " .
  88. '$this->setDbTable(\'' . $this->_appNamespace . '_Model_DbTable_' . $this->_tableName . '\');' . "\n" .
  89. '}' . "\n" .
  90. 'return $this->_dbTable;',
  91. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  92. 'shortDescription' => 'getDbTable',
  93. 'tags' => array(
  94. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  95. 'datatype' => 'object',
  96. )),
  97. ),
  98. )),
  99. );
  100. return $methods;
  101. }
  102. protected function fetchAllSql()
  103. {
  104. $method = array(
  105. 'name' => 'fetchAll',
  106. 'body' => '$resultSet = $this->getDbTable()->fetchAll();' . "\n" .
  107. '$entries = array();' . "\n" .
  108. 'foreach ($resultSet AS $row) {' . "\n " .
  109. '$entry = new ' . $this->_appNamespace . '_Model_' . $this->_tableName . '();' . "\n " .
  110. '$this->_populateModel($row, $entry);' . "\n " .
  111. '$entries[] = $entry;' . "\n" .
  112. '}' . "\n" .
  113. 'return $entries;',
  114. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  115. 'shortDescription' => 'fetchAll',
  116. 'tags' => array(
  117. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  118. 'datatype' => 'Array',
  119. )),
  120. ),
  121. )),
  122. );
  123. return $method;
  124. }
  125. protected function deleteSql()
  126. {
  127. $primaryGet = $this->fixColumnName($this->_primaryKey, true);
  128. $method = array(
  129. 'name' => 'delete',
  130. 'parameters' => array(
  131. array(
  132. 'name' => 'model',
  133. 'type' => $this->_appNamespace . '_Model_' . $this->_tableName,
  134. ),
  135. ),
  136. 'body' => '$this->getDbTable()->delete(array(\'' . $this->_primaryKey . ' = ?\' => $model->get' . $primaryGet . '()));',
  137. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  138. 'shortDescription' => 'delete',
  139. 'tags' => array(
  140. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  141. 'datatype' => 'Array',
  142. )),
  143. ),
  144. )),
  145. );
  146. return $method;
  147. }
  148. protected function findSql()
  149. {
  150. $method = array(
  151. 'name' => 'find',
  152. 'parameters' => array(
  153. array(
  154. 'name' => 'id',
  155. ),
  156. array(
  157. 'name' => 'model',
  158. 'type' => $this->_appNamespace . '_Model_' . $this->_tableName,
  159. ),
  160. ),
  161. 'body' => '$result = $this->getDbTable()->find($id);' . "\n" .
  162. 'if (0 == count($result)) {' . "\n " .
  163. 'return;' . "\n" .
  164. '}' . "\n" .
  165. '$row = $result->current();' . "\n" .
  166. '$this->_populateModel($row, $model);',
  167. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  168. 'shortDescription' => 'find',
  169. 'tags' => array(
  170. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  171. 'datatype' => 'object',
  172. )),
  173. ),
  174. )),
  175. );
  176. return $method;
  177. }
  178. protected function insertSql()
  179. {
  180. $primaryGet = $this->fixColumnName($this->_primaryKey, true);
  181. $method = array(
  182. 'name' => 'insert',
  183. 'parameters' => array(
  184. array(
  185. 'name' => 'model',
  186. 'type' => $this->_appNamespace . '_Model_' . $this->_tableName,
  187. ),
  188. ),
  189. 'body' => $this->_insertSaveArray . "\n" .
  190. '$id = $this->getDbTable()->insert($data);' . "\n" .
  191. '$model->set' . $primaryGet . '($id);',
  192. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  193. 'shortDescription' => 'insert',
  194. 'tags' => array(
  195. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  196. 'datatype' => 'object',
  197. )),
  198. ),
  199. )),
  200. );
  201. return $method;
  202. }
  203. protected function saveSql()
  204. {
  205. $primaryGet = $this->fixColumnName($this->_primaryKey, true);
  206. $method = array(
  207. 'name' => 'update',
  208. 'parameters' => array(
  209. array(
  210. 'name' => 'model',
  211. 'type' => $this->_appNamespace . '_Model_' . $this->_tableName,
  212. ),
  213. ),
  214. 'body' => $this->_insertSaveArray . "\n" .
  215. '$id = $model->get' . $primaryGet . '();' . "\n" .
  216. '$this->getDbTable()->update($data, array(\'' . $this->_primaryKey . ' = ?\' => $id));',
  217. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  218. 'shortDescription' => 'update',
  219. 'tags' => array(
  220. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  221. 'datatype' => 'object',
  222. )),
  223. ),
  224. )),
  225. );
  226. return $method;
  227. }
  228. protected function generatePopulateSetters($fields)
  229. {
  230. $pop = "\$model";
  231. $i = count($fields);
  232. $c = 1;
  233. foreach ($fields as $field) {
  234. $pop .= "->set" . $field['Name'] . "(\$row->" . $field['Database_Column_Name']. ")";
  235. if ($c < $i ) {
  236. $pop .= "\n ";
  237. }
  238. $c++;
  239. }
  240. $pop .= ";\n return;";
  241. $this->_populateSetters = $pop;
  242. }
  243. protected function generateInsertSaveArray($fields)
  244. {
  245. $insertSave = '$data = array(' . "\n\t";
  246. $c = count($fields);
  247. $i = 1;
  248. foreach ($fields as $field) {
  249. $insertSave .= "'" . $field['Database_Column_Name'] ."'" . ' => $model->get' .
  250. $this->fixColumnName($field['Database_Column_Name']) . '(),' . "\n";
  251. if ($i < $c) {
  252. $insertSave .= "\t";
  253. }
  254. $i++;
  255. }
  256. $insertSave .= ');';
  257. $this->_insertSaveArray = $insertSave;
  258. }
  259. protected function populateMethod()
  260. {
  261. $method = array(
  262. 'name' => '_populateModel',
  263. 'parameters' => array(
  264. array(
  265. 'name' => 'row',
  266. 'type' => 'Zend_Db_Table_Row_Abstract'
  267. ),
  268. array(
  269. 'name' => 'model',
  270. 'type' => $this->_appNamespace . '_Model_' . $this->_tableName,
  271. ),
  272. ),
  273. 'body' => $this->_populateSetters,
  274. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  275. 'shortDescription' => 'Populate Model',
  276. 'tags' => array(
  277. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  278. 'datatype' => 'void',
  279. )),
  280. ),
  281. )),
  282. );
  283. return $method;
  284. }
  285. protected function createMapper()
  286. {
  287. $dbTableMethods = $this->dbTableMethods();
  288. $insertSql = $this->insertSql();
  289. $saveSql = $this->saveSql();
  290. $findSql = $this->findSql();
  291. $fetchAllSql = $this->fetchAllSql();
  292. $deleteSql = $this->deleteSql();
  293. $populateModel = $this->populateMethod();
  294. $mapperClass = new Zend_CodeGenerator_Php_Class();
  295. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  296. 'shortDescription' => 'Table Mapper ' . $this->_tableNameOriginal,
  297. 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
  298. 'tags' => array(
  299. array(
  300. 'name' => 'version',
  301. 'description' => '$Rev:$',
  302. ),
  303. ),
  304. ));
  305. $mapperClass->setName($this->_appNamespace . '_Model_Mapper_' . $this->_tableName )
  306. ->setDocblock($docblock)
  307. ->setProperty(
  308. array(
  309. 'name' => '_dbTable',
  310. 'visibility' => 'protected',
  311. 'defaultValue' => '',)
  312. )
  313. ->setMethods($dbTableMethods)
  314. ->setMethod($insertSql)
  315. ->setMethod($saveSql)
  316. ->setMethod($findSql)
  317. ->setMethod($fetchAllSql)
  318. ->setMethod($deleteSql)
  319. ->setMethod($populateModel);
  320. return $mapperClass;
  321. }
  322. protected function saveMapperFile($code)
  323. {
  324. $file = new Zend_CodeGenerator_Php_File(array(
  325. 'classes' => array($code),
  326. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  327. 'shortDescription' => 'Mapper class file',
  328. 'tags' => array(
  329. array(
  330. 'name' => 'license',
  331. 'description' => 'New BSD',
  332. ),
  333. ),
  334. )),
  335. ));
  336. $filename = $this->_path . '/' . $this->_tableName . '.php';
  337. $f = fopen($filename, 'w');
  338. if (!$f) {
  339. return false;
  340. } else {
  341. $bytes = fwrite($f, $file);
  342. fclose($f);
  343. $this->_logger->info('Created Mapper' . $this->_tableName . ' @ ' . $filename . ' <br />');
  344. return $bytes;
  345. }
  346. }
  347. }