Skip to content

Instantly share code, notes, and snippets.

@bjyoungblood
Created May 14, 2012 21:39
Show Gist options
  • Save bjyoungblood/2697410 to your computer and use it in GitHub Desktop.
Save bjyoungblood/2697410 to your computer and use it in GitHub Desktop.
ZF2 Database-to-PHP-Model Generator
<?php
use Zend\Loader\StandardAutoloader as Autoloader,
Zend\Db\Metadata\Metadata,
Zend\Db\Adapter\Adapter as DbAdapter;
/////////////////////////////////////
// START CONFIGURATION
/////////////////////////////////////
//
define('ZF2_PATH', '/path/to/zf2');
define('TAB', ' ');
//define('TAB', "\t");
$moduleName = 'YourModule';
$dbconfig = array(
'driver' => 'Mysqli',
'hostname' => 'localhost',
'username' => 'changeme',
'password' => 'changeme',
'database' => 'changeme',
'table_type' => 'InnoDB'
);
/////////////////////////////////////
// END CONFIGURATION
/////////////////////////////////////
require_once ZF2_PATH . '/library/Zend/Loader/StandardAutoloader.php';
$autoloader = new Autoloader;
$autoloader->register();
class ModelBuilder {
protected $db;
protected $stdin;
protected $module;
public function __construct($dbParams, $module) {
$this->db = new DbAdapter($dbParams);
$this->module = $module;
$this->stdin = fopen('php://stdin', 'r');
}
public function run() {
$tables = $this->getTablesToModel();
$files = $this->getFiles($tables);
$this->writeFiles($files);
}
public function getTablesToModel() {
$metadata = new Metadata($this->db);
foreach ($metadata->getTables() as $t) {
if ($this->prompt($t->getName())) {
$tables[] = $t;
}
}
return $tables;
}
public function writeFiles($files) {
foreach ($files as $dirname => $dir) {
foreach ($dir as $file => $content) {
$filename = $dirname . '/' . $file;
@mkdir(dirname($filename), 0777, true);
file_put_contents($filename, $content);
}
}
}
public function getFiles($tables) {
foreach ($tables as $t) {
$modelName = $this->toCamelCase($t->getName());
$modelInterfaceName = $modelName . 'Interface';
$mapperName = $modelName . 'Mapper';
$mapperInterfaceName = $modelName . 'MapperInterface';
$modelFile = <<<EOF
<?php
namespace {$this->module}\Model\\$modelName;
use ZfcBase\Model\ModelAbstract;
class $modelName extends ModelAbstract implements $modelInterfaceName
{
EOF;
// fields
foreach ($t->getColumns() as $col) {
$colName = $this->toCamelCase($col->getName());
$colName[0] = strtolower($colName[0]);
$modelFile .= TAB . "protected \${$colName};\n";
}
$modelFile .= "\n";
// getters and setters
foreach ($t->getColumns() as $col) {
$colName = $this->toCamelCase($col->getName());
$colFuncName = $colName;
$colName[0] = strtolower($colName[0]);
$modelFile .= TAB . "public function get{$colFuncName}()\n";
$modelFile .= TAB . "{\n";
$modelFile .= TAB . TAB . "return \$this->$colName;\n";
$modelFile .= TAB . "}\n\n";
$modelFile .= TAB . "public function set{$colFuncName}(\$$colName)\n";
$modelFile .= TAB . "{\n";
$modelFile .= TAB . TAB . "\$this->$colName = \$$colName;\n";
$modelFile .= TAB . TAB . "return \$this;\n";
$modelFile .= TAB . "}\n\n";
}
$modelFile .= "}";
$files[$modelName][$modelName . '.php'] = $modelFile;
/**
* MODEL INTERFACE START
*/
$iModelFile = <<<EOF
<?php
namespace {$this->module}\Model\\$modelName;
interface $modelInterfaceName
{
EOF;
foreach ($t->getColumns() as $col) {
$colName = $this->toCamelCase($col->getName());
$colFuncName = $colName;
$colName[0] = strtolower($colName[0]);
$iModelFile .= TAB . "public function get{$colFuncName}();\n";
$iModelFile .= TAB . "public function set{$colFuncName}(\$$colName);\n";
}
$iModelFile .= "}";
$files[$modelName][$modelInterfaceName . '.php'] = $iModelFile;
/**
* MAPPER INTERFACE START
*/
$iMapperFile = <<<EOF
<?php
namespace {$this->module}\Model\\$modelName;
interface $mapperInterfaceName
{
}
EOF;
$files[$modelName][$mapperInterfaceName . '.php'] = $iMapperFile;
/**
* MAPPER START
*/
$mapperFile = <<<EOF
<?php
namespace {$this->module}\Model\\$modelName;
use ZfcBase\Mapper\DbMapperAbstract;
class $mapperName extends DbMapperAbstract implements $mapperInterfaceName
{
protected \$tableName = '{$t->getName()}';
}
EOF;
$files[$modelName][$mapperName . '.php'] = $mapperFile;
}
return $files;
}
protected function prompt($tableName) {
echo "Do you want to model this table ($tableName)? (y/N) ";
$answer = fgets($this->stdin, 256);
if (strtolower($answer[0]) === 'y') {
return true;
} else {
return false;
}
}
protected function toCamelCase($name)
{
return implode('',array_map('ucfirst', explode('_',$name)));
}
protected static function fromCamelCase($name)
{
return trim(preg_replace_callback('/([A-Z])/', function($c){ return '_'.strtolower($c[1]); }, $name),'_');
}
}
$builder = new ModelBuilder($dbconfig, $moduleName);
$builder->run();
@tirsope
Copy link

tirsope commented Nov 29, 2012

Hi, very nice wrk, how do I use it?

@NeftaliYagua
Copy link

After config run
php ModelBuilder.php

@misilot
Copy link

misilot commented Oct 7, 2013

I'm getting the following error

PHP Fatal error: Class 'Zend\Db\Adapter\Adapter' not found in /home/user/Models/ModelBuilder.php on line 40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment