/My/Scripts/DbModel.php

https://github.com/mingomax/Zend-Db-Model-Creation · PHP · 418 lines · 385 code · 28 blank · 5 comment · 3 complexity · d6a926567819b7eb4c83d4fe2f656f6d MD5 · raw file

  1. <?php
  2. class My_Scripts_DbModel extends My_Scripts_ModelCreation
  3. {
  4. protected $_tableNameOriginal;
  5. protected $_tableName;
  6. protected $_properties;
  7. protected $_getterSetters;
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. $this->_logger->info('Create Db Model <br />');
  12. $this->generateModel();
  13. }
  14. protected function generateModel()
  15. {
  16. //get the tables in the database
  17. $dbTables = $this->getDbTables();
  18. //get the describe of the table
  19. foreach ($dbTables as $table) {
  20. $describeTable = $this->describeTable($table);
  21. if ($describeTable) {
  22. $this->_tableNameOriginal = $table;
  23. $this->_tableName = Zend_Filter::filterStatic($table, 'StringToLower');
  24. $this->_tableName = Zend_Filter::filterStatic($this->_tableName, 'Word_UnderscoreToCamelCase');
  25. $fields = array();
  26. foreach ($describeTable as $tableMeta)
  27. {
  28. $fields[] = array(
  29. 'Name' => (string) $this->fixColumnName($tableMeta['COLUMN_NAME']),
  30. 'Data_Type' => (string) $tableMeta['DATA_TYPE'],
  31. 'Nullable' => (bool) $tableMeta["NULLABLE"],
  32. );
  33. }
  34. $this->createProperties($fields);
  35. $this->getterAndSetterMethods($fields);
  36. $code = $this->createModel();
  37. $this->saveModelFile($code);
  38. }
  39. }
  40. }
  41. public function createProperties($fields)
  42. {
  43. $properties = array();
  44. //add mapper
  45. $fields[] = array('Name' => 'mapper', 'Data_Type' => '');
  46. foreach ($fields as $field) {
  47. $properties[] = array(
  48. 'name' => '_' . $field['Name'],
  49. 'visibility' => 'protected',
  50. 'defaultValue' => '',
  51. );
  52. }
  53. $this->_properties = $properties;
  54. }
  55. public function getterAndSetterMethods($fields)
  56. {
  57. $methods = array();
  58. foreach ($fields as $field) {
  59. $dataType = $this->phpDataTypeConversion($field['Data_Type']);
  60. // Create Setter
  61. $methods[] = array(
  62. 'name' => 'set' . ucfirst($field['Name']),
  63. 'parameters' => array(
  64. array(
  65. 'name' => $field['Name']
  66. ),
  67. ),
  68. 'body' => '$this->_' . $field['Name'] . ' = (' . $dataType . ') $' . $field['Name'] . ';' . "\n" . 'return $this;',
  69. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  70. 'shortDescription' => 'Set ' . $field['Name'] . ' property',
  71. 'tags' => array(
  72. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  73. 'paramName' => $field['Name'],
  74. 'datatype' => $dataType,
  75. )),
  76. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  77. 'datatype' => $dataType,
  78. )),
  79. ),
  80. )),
  81. );
  82. // Create Getter
  83. $methods[] = array(
  84. 'name' => 'get' . ucfirst($field['Name']),
  85. 'body' => 'return $this->_' . $field['Name'] . ';',
  86. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  87. 'shortDescription' => 'Get ' . $field['Name'] . ' property',
  88. 'tags' => array(
  89. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  90. 'datatype' => $dataType,
  91. )),
  92. ),
  93. )),
  94. );
  95. }
  96. $this->_getterSetters = $methods;
  97. }
  98. protected function _buildConstructor()
  99. {
  100. $constructor= array(
  101. 'name' => '__construct',
  102. 'parameters' => array(
  103. array(
  104. 'name' => 'options',
  105. 'defaultValue' => null,
  106. ),
  107. ),
  108. 'body' => 'if (is_array($options)) {' . "\n " . '$this->setOptions($options);' . "\n" . '}',
  109. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  110. 'shortDescription' => 'constructor',
  111. 'tags' => array(
  112. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  113. 'paramName' => 'options',
  114. 'datatype' => 'array',
  115. )),
  116. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  117. 'datatype' => 'void',
  118. )),
  119. ),
  120. )),
  121. );
  122. return $constructor;
  123. }
  124. protected function _buildMagicSet()
  125. {
  126. $magicSet = array(
  127. 'name' => '__set',
  128. 'parameters' => array(
  129. array(
  130. 'name' => 'name',
  131. ),
  132. array(
  133. 'name' => 'value',
  134. ),
  135. ),
  136. 'body' => '$method = \'set\' . ucfirst($name);' . "\n" .
  137. 'if ((\'mapper\' == $name) || !method_exists($this, $method)) {' . "\n " .
  138. 'throw new Exception(\'Invalid property\');' . "\n" .
  139. '}' . "\n" .
  140. '$this->$method($value);',
  141. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  142. 'shortDescription' => 'setter',
  143. 'tags' => array(
  144. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  145. 'paramName' => 'name',
  146. 'datatype' => 'string',
  147. )),
  148. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  149. 'paramName' => 'value',
  150. 'datatype' => 'string',
  151. )),
  152. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  153. 'datatype' => 'object',
  154. )),
  155. ),
  156. )),
  157. );
  158. return $magicSet;
  159. }
  160. protected function _buildMagicGet()
  161. {
  162. $magicGet = array(
  163. 'name' => '__get',
  164. 'parameters' => array(
  165. array(
  166. 'name' => 'name',
  167. ),
  168. ),
  169. 'body' => '$method = \'get\' . ucfirst($name);' . "\n" .
  170. 'if ((\'mapper\' == $name) || !method_exists($this, $method)) {' . "\n " .
  171. 'throw new Exception(\'Invalid property\');' . "\n" .
  172. '}' . "\n" .
  173. '$this->$method();',
  174. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  175. 'shortDescription' => 'getter',
  176. 'tags' => array(
  177. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  178. 'paramName' => 'name',
  179. 'datatype' => 'string',
  180. )),
  181. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  182. 'datatype' => 'object',
  183. )),
  184. ),
  185. )),
  186. );
  187. return $magicGet;
  188. }
  189. protected function _buildSetOptions()
  190. {
  191. $setOptions = array(
  192. 'name' => 'setOptions',
  193. 'parameters' => array(
  194. array(
  195. 'name' => 'options',
  196. ),
  197. ),
  198. 'body' => '$methods = get_class_methods($this);' . "\n" .
  199. 'foreach ($options as $key => $value) {' . "\n " .
  200. '$method = \'set\' . ucfirst($key);' . "\n " .
  201. 'if (in_array($method, $methods)) {' . "\n " .
  202. '$this->$method($value);' . "\n " .
  203. '}' . "\n" .
  204. '}' . "\n" .
  205. 'return $this;',
  206. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  207. 'shortDescription' => 'setOptions',
  208. 'tags' => array(
  209. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  210. 'paramName' => 'options',
  211. 'datatype' => 'array',
  212. )),
  213. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  214. 'datatype' => 'object',
  215. )),
  216. ),
  217. )),
  218. );
  219. return $setOptions;
  220. }
  221. protected function _buildMapperMethods() {
  222. $mapperMethods[] = array(
  223. 'name' => 'getMapper',
  224. 'body' => 'if (null === $this->_mapper) {' . "\n " .
  225. '$this->setMapper(new ' . $this->_appNamespace . '_Model_Mapper_' . $this->_tableName . '());' . "\n" .
  226. '}' . "\n" .
  227. 'return $this->_mapper;',
  228. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  229. 'shortDescription' => 'getMapper for ' . $this->_tableName,
  230. 'tags' => array(
  231. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  232. 'datatype' => 'object',
  233. )),
  234. ),
  235. )),
  236. );
  237. $mapperMethods[] = array(
  238. 'name' => 'setMapper',
  239. 'parameters' => array(
  240. array(
  241. 'name' => 'mapper',
  242. ),
  243. ),
  244. 'body' => '$this->_mapper = $mapper;' . "\n" .
  245. 'return $this;',
  246. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  247. 'shortDescription' => 'setMapper',
  248. 'tags' => array(
  249. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  250. 'paramName' => 'mapper',
  251. 'datatype' => 'object',
  252. )),
  253. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  254. 'datatype' => 'object',
  255. )),
  256. ),
  257. )),
  258. );
  259. return $mapperMethods;
  260. }
  261. protected function _buildMapperSQLMethods()
  262. {
  263. $mapperSQLMethods[] = array(
  264. 'name' => 'delete',
  265. 'body' => '$this->getMapper()->delete($this);' . "\n" . 'return $this;',
  266. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  267. 'shortDescription' => 'delete',
  268. 'tags' => array(
  269. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  270. 'datatype' => 'object',
  271. )),
  272. ),
  273. )),
  274. );
  275. $mapperSQLMethods[] = array(
  276. 'name' => 'add',
  277. 'body' => '$this->getMapper()->add($this);' . "\n" . 'return $this;',
  278. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  279. 'shortDescription' => 'add',
  280. 'tags' => array(
  281. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  282. 'datatype' => 'object',
  283. )),
  284. ),
  285. )),
  286. );
  287. $mapperSQLMethods[] = array(
  288. 'name' => 'save',
  289. 'body' => '$this->getMapper()->update($this);' . "\n" . 'return $this;',
  290. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  291. 'shortDescription' => 'save',
  292. 'tags' => array(
  293. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  294. 'datatype' => 'object',
  295. )),
  296. ),
  297. )),
  298. );
  299. $mapperSQLMethods[] = array(
  300. 'name' => 'find',
  301. 'parameters' => array(
  302. array(
  303. 'name' => 'id',
  304. ),
  305. ),
  306. 'body' => '$this->getMapper()->find($id, $this);' . "\n" . 'return $this;',
  307. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  308. 'shortDescription' => 'find',
  309. 'tags' => array(
  310. new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
  311. 'paramName' => 'id',
  312. 'datatype' => 'int',
  313. )),
  314. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  315. 'datatype' => 'object',
  316. )),
  317. ),
  318. )),
  319. );
  320. $mapperSQLMethods[] = array(
  321. 'name' => 'fetchAll',
  322. 'body' => 'return $this->getMapper()->fetchAll();',
  323. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  324. 'shortDescription' => 'fetchAll',
  325. 'tags' => array(
  326. new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
  327. 'datatype' => 'array',
  328. )),
  329. ),
  330. )),
  331. );
  332. return $mapperSQLMethods;
  333. }
  334. public function createModel()
  335. {
  336. $constructor = $this->_buildConstructor();
  337. $magicSet = $this->_buildMagicSet();
  338. $magicGet = $this->_buildMagicGet();
  339. $setOptions = $this->_buildSetOptions();
  340. $mapperMethods = $this->_buildMapperMethods();
  341. $mapperSql = $this->_buildMapperSQLMethods();
  342. $modelClass = new Zend_CodeGenerator_Php_Class();
  343. $docblock = new Zend_CodeGenerator_Php_Docblock(array(
  344. 'shortDescription' => 'Table ' . $this->_tableNameOriginal,
  345. 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
  346. 'tags' => array(
  347. array(
  348. 'name' => 'version',
  349. 'description' => '$Rev:$',
  350. ),
  351. ),
  352. ));
  353. $modelClass->setName($this->_appNamespace . '_Model_' . $this->_tableName )
  354. ->setDocblock($docblock)
  355. ->setProperties($this->_properties)
  356. ->setMethod($constructor)
  357. ->setMethod($magicSet)
  358. ->setMethod($magicGet)
  359. ->setMethod($setOptions)
  360. ->setMethods($mapperMethods)
  361. ->setMethods($this->_getterSetters)
  362. ->setMethods($mapperSql);
  363. return $modelClass;
  364. }
  365. protected function saveModelFile($code)
  366. {
  367. $file = new Zend_CodeGenerator_Php_File(array(
  368. 'classes' => array($code),
  369. 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
  370. 'shortDescription' => 'Foo class file',
  371. 'tags' => array(
  372. array(
  373. 'name' => 'license',
  374. 'description' => 'New BSD',
  375. ),
  376. ),
  377. )),
  378. ));
  379. $filename = APPLICATION_PATH . '/models/'. $this->_tableName . '.php';
  380. $f = fopen($filename, 'w');
  381. if (!$f) {
  382. return false;
  383. } else {
  384. $bytes = fwrite($f, $file);
  385. fclose($f);
  386. $this->_logger->info('Created Model ' . $this->_tableName . ' @ ' . $filename . ' <br />');
  387. return $bytes;
  388. }
  389. }
  390. }