Advertisement
Guest User

Untitled

a guest
May 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. <?php
  2. /**
  3. * phalcon pdo tests
  4. *
  5. */
  6. ini_set('display_errors', 1);
  7. error_reporting(E_ALL);
  8.  
  9. $nameSpace = 'Desk3';
  10.  
  11. $tableName = @$argv[1];
  12. $saveLocal = @$argv[2];
  13.  
  14. if (empty($tableName)) {
  15. echo 'usage: ' . PHP_EOL;
  16. echo " $> php {$argv[0]} <tablename> [test]" . PHP_EOL;
  17. die();
  18. }
  19.  
  20. $cfg = parse_ini_file('./app/config/config.ini', true, INI_SCANNER_TYPED);
  21.  
  22. if (empty($cfg)) {
  23. echo 'unable to parse ./app/config/config.ini' . PHP_EOL;
  24. die();
  25. }
  26.  
  27. $className = str_replace('_', '', ucwords($tableName, '_'));
  28.  
  29. if ($saveLocal != 'test') {
  30. $modelName = "{$cfg['application']['modelsDir']}{$nameSpace}/{$className}.php";
  31.  
  32. if (file_exists($modelName)) {
  33. echo "model `{$className}` already exists" . PHP_EOL;
  34. die();
  35. }
  36. } else {
  37. $modelName = "./test_{$className}.php";
  38. }
  39.  
  40. //$cfg = [
  41. // 'dsn' => 'mysql',
  42. // 'host' => 'localhost',
  43. // 'port' => 3406,
  44. // 'schema' => 'coo_maria',
  45. // 'username' => 'root',
  46. // 'password' => 'mariar00t',
  47. // 'charset' => 'utf8'
  48. //];
  49.  
  50.  
  51. $tranz = [
  52. 'LONGLONG' => 'integer',
  53. 'LONG' => 'integer',
  54. 'TINY' => 'integer',
  55. 'VAR_STRING' => 'string',
  56. 'BLOB' => 'string',
  57. 'DATETIME' => 'string',
  58. 'NEWDECIMAL' => 'double',
  59. 'DOUBLE' => 'double',
  60. 'DATE' => 'string',
  61. 'INT24' => 'integer',
  62. 'TIMESTAMP' => 'string'
  63. ];
  64.  
  65. $db = new PDO(
  66. "{$cfg['database']['adapter']}:host={$cfg['database']['host']};port={$cfg['database']['port']};dbname={$cfg['database']['dbname']};charset={$cfg['database']['charset']}",
  67. "{$cfg['database']['username']}",
  68. "{$cfg['database']['password']}",
  69. [
  70. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  71. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  72. PDO::ATTR_EMULATE_PREPARES => false,
  73. PDO::ATTR_PERSISTENT => true
  74. ]
  75. );
  76.  
  77. $tableMeta = [];
  78.  
  79. $result = $db->query("select * from {$tableName} limit 0");
  80. foreach (range(0, $result->columnCount()-1) as $columnIndex) {
  81. $x = $result->getColumnMeta($columnIndex);
  82. // print_r ($x);
  83. // echo PHP_EOL . PHP_EOL;
  84.  
  85. $tableMeta[$columnIndex]['type'] = @$tranz[$x['native_type']] ?: 'string';
  86. $tableMeta[$columnIndex]['ispk'] = in_array('primary_key', $x['flags']); // ? 'true' : 'false';
  87. $tableMeta[$columnIndex]['nullable'] = !in_array('not_null', $x['flags']) ? 'true' : 'false';
  88. $tableMeta[$columnIndex]['size'] = ($tableMeta[$columnIndex]['type'] == 'string') ? intval($x['len'] / 3) : intval($x['len']);
  89. if (in_array($x['native_type'], ['BLOB', 'DATETIME', 'DATE', 'TIMESTAMP'])) {
  90. $tableMeta[$columnIndex]['size'] = -1;
  91. }
  92. $tableMeta[$columnIndex]['fieldName'] = $x['name'];
  93. $tableMeta[$columnIndex]['setName'] = 'set' . str_replace('_', '', ucwords($x['name'], '_'));
  94. $tableMeta[$columnIndex]['getName'] = 'get' . str_replace('_', '', ucwords($x['name'], '_'));
  95. }
  96.  
  97. $db = null;
  98.  
  99. //print_r($tableMeta);
  100. //die();
  101.  
  102. $timestamp = date('Y-m-d, H:i:s');
  103. $i = 0;
  104.  
  105. ob_start();
  106. // namespace
  107. echo "<?php\n\nnamespace Desk3;\n\n";
  108.  
  109. // start class
  110. echo "/**\n * {$className}\n *\n * @package Desk3\n * @autogenerated by Gavin Developer Tools\n * @date {$timestamp}\n */\n";
  111. echo "class {$className} extends BaseModel\n{\n\n";
  112.  
  113. $primaryKey = '';
  114.  
  115. // place member variables
  116. foreach ($tableMeta as $fieldMeta) {
  117. echo "\t/**\n\t *\n\t * @var {$fieldMeta['type']}\n";
  118.  
  119. if ($fieldMeta['ispk'] == true) {
  120. echo "\t * @Primary\n\t * @Identity\n";
  121. $primaryKey = $fieldMeta['fieldName'];
  122. }
  123.  
  124. echo "\t * @Column(type=\"{$fieldMeta['type']}\", ";
  125. if ($fieldMeta['size'] > 0) {
  126. echo "length={$fieldMeta['size']}, ";
  127. }
  128. echo "nullable={$fieldMeta['nullable']})\n\t */\n";
  129.  
  130. echo "\tprotected \${$fieldMeta['fieldName']};\n\n";
  131. }
  132.  
  133. echo "\n";
  134.  
  135. // place setters
  136. foreach ($tableMeta as $fieldMeta) {
  137. echo "\t/**\n\t * Method to set the value of field {$fieldMeta['fieldName']}\n\t *\n";
  138. echo "\t * @param {$fieldMeta['type']} \${$fieldMeta['fieldName']}\n";
  139. echo "\t * @return \$this\n\t */\n";
  140.  
  141. echo "\tpublic function {$fieldMeta['setName']}(\${$fieldMeta['fieldName']})\n\t{\n";
  142. echo "\t\t\$this->{$fieldMeta['fieldName']} = \${$fieldMeta['fieldName']};\n\n";
  143. echo "\t\treturn \$this;\n\t}\n\n";
  144. }
  145.  
  146. echo "\n";
  147.  
  148. // place getters
  149. foreach ($tableMeta as $fieldMeta) {
  150. echo "\t/**\n\t * Returns the value of field {$fieldMeta['fieldName']}\n\t *\n";
  151. echo "\t * @return {$fieldMeta['type']}\n\t */\n";
  152.  
  153. echo "\tpublic function {$fieldMeta['getName']}()\n\t{\n";
  154. echo "\t\treturn \$this->{$fieldMeta['fieldName']};\n\t}\n\n";
  155. }
  156.  
  157. echo "\n";
  158.  
  159. // place initialize
  160. echo "\t/**\n\t * Initialize method for model.\n\t */\n";
  161. echo "\tpublic function initialize()\n\t{\n";
  162. echo "\t\tparent::initialize();\n\n\t\t\$this->setSource('{$tableName}');\n\n";
  163.  
  164. if (!empty($primaryKey)) {
  165. echo "\t\t// skip primary key on create and update as it is auto-create\n";
  166. echo "\t\t\$this->skipAttributes([\n\t\t\t'{$primaryKey}'\n\t\t]);\n";
  167. }
  168.  
  169. echo "\t}\n";
  170.  
  171.  
  172. // end class
  173. echo "}\n";
  174.  
  175. $contents = ob_get_contents();
  176. ob_end_clean();
  177.  
  178. file_put_contents($modelName, $contents);
  179.  
  180. echo ("{$className} generated");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement