/** * @todo Manage default values + primary keys */ public function generate($filePath = null) { if (!file_exists($filePath)) { return false; } $info = pathinfo($filePath); $schema = new MySQLWorkbench($info['filename'], $info['dirname']); $xml = $schema->noHeader()->run()->render(); $array = Xml::toArray(Xml::build($xml)); $tables = array(); foreach ($array['schema']['table'] as $table) { $tables[$table['@name']] = array(); if (!isset($table['column'][0])) { $keys = array('type', 'isPrimary', 'auto', 'isRequired', 'size', 'index'); $col = array(); foreach ($keys as $key) { $col[$key] = isset($table['column']['@' . $key]) ? $table['column']['@' . $key] : null; } $tables[$table['@name']][$table['column']['@name']] = $col; } else { foreach ($table['column'] as $_col) { $keys = array('type', 'isPrimary', 'auto', 'isRequired', 'size', 'index'); $col = array(); foreach ($keys as $key) { $col[$key] = isset($_col['@' . $key]) ? $_col['@' . $key] : null; } $tables[$table['@name']][$_col['@name']] = $col; } } } $schemaName = Inflector::camelize($info['filename']); $types = array('int' => 'integer', 'varchar' => 'string'); $_tables = array(); foreach ($tables as $table => $fields) { $_fields = array(); foreach ($fields as $field => $conf) { $_conf = array(); if (isset($conf['type']) && !empty($conf['type'])) { if (isset($types[$conf['type']])) { $conf['type'] = $types[$conf['type']]; } $_conf[] = "'type' => '{$conf['type']}'"; } if (isset($conf['size']) && !empty($conf['size']) && $conf['size'] != -1) { $_conf[] = "'length' => {$conf['size']}"; } if (!isset($conf['isRequired']) && empty($conf['isRequired'])) { $_conf[] = "'null' => false"; } else { $_conf[] = "'null' => true"; $_conf[] = "'default' => null"; } if (isset($conf['isPrimary']) && !empty($conf['isPrimary'])) { $_conf[] = "'key' => 'primary'"; } $_fields[] = "\t\t'{$field}' => array(" . join(', ', $_conf) . ')'; } $_tables[] = "\t'{$table}' => array(" . PHP_EOL . join(',' . PHP_EOL, $_fields) . PHP_EOL . "\t)"; } $content = '<?php' . PHP_EOL . PHP_EOL . 'class ' . $schemaName . 'Schema extends CakeSchema {' . PHP_EOL . PHP_EOL . join(',' . PHP_EOL, $_tables) . PHP_EOL . PHP_EOL . '}'; $schemaPath = $info['dirname'] . DS . $info['filename'] . '.php'; file_put_contents($schemaPath, $content); return $schemaPath; }
<?php include_once 'MySQLWorkbenchXML.class.php'; include_once 'MySQLWorkbench.class.php'; // Download my sample mwb-file from http://www.query4u.de/showcase/tests/Workbench/user.mwb // or create your own sample by using mysql workbench. // The mwb-file is not part of this sample, because phpclasses does allow to upload mwb-files. $user = new MySQLWorkbench("user", dirname(__FILE__)); $user->run(); echo $user->render(); die;