Skip to content

Instantly share code, notes, and snippets.

@diguinhorocks
Created June 2, 2012 19:33

Revisions

  1. diguinhorocks revised this gist Jun 19, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MysqlTableMapper.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    <?php

    class MysqlTableMapper{
    class MysqlMapper{

    protected static $instance = null;
    protected $pdo;
  2. diguinhorocks revised this gist Jun 2, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gen.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    <?php

    include('MysqlMapper.php');
    include('ModelGen.php');

    $gen = new ModelGen();

    $gen->setHost('localhost');
  3. diguinhorocks revised this gist Jun 2, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ModelGen.php
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ public function setDatabase($database){
    }

    public function setHost($host){
    parent:HOST = $host;
    parent::HOST = $host;
    }

    public function setUser($user){
  4. diguinhorocks revised this gist Jun 2, 2012. 3 changed files with 30 additions and 5 deletions.
    23 changes: 20 additions & 3 deletions ModelGen.php
    Original file line number Diff line number Diff line change
    @@ -6,12 +6,14 @@ class ModelGen extends MysqlMapper{

    protected $model = null;

    protected $extends = ' extends MysqlActiveRecord';
    protected $extends = 'extends MysqlActiveRecord';

    protected $implements = '';

    protected $mapper = null;

    const PATH = '';

    public function __construct(){
    $this->mapper = parent::getInstance();
    }
    @@ -24,10 +26,25 @@ public function setDatabase($database){
    parent::DATABASE = $database;
    }

    public function setHost($host){
    parent:HOST = $host;
    }

    public function setUser($user){
    parent::USER = $user;
    }

    public function setPassword($password){
    parent::PASS = $password;
    }

    public function setPath($path){
    self::PATH = $path;
    }

    protected function createModelHeader(){

    $this->model .= "<?php ". $this->printEndOfLine(2) ." class ".($this->tableToClassName($this->modelName)) . $this->extends . $this->implements . "{ " . $this->printEndOfLine(2) ;
    $this->model .= "<?php ". $this->printEndOfLine(2) ." class ".($this->tableToClassName($this->modelName)) . ' '.$this->extends . ' '.$this->implements . "{ " . $this->printEndOfLine(2) ;

    }
    protected function createModelFooter(){
    @@ -86,7 +103,7 @@ public function generate(){
    }

    protected function writeInFile($title, $content){
    $fp = fopen(MODEL. strtolower($title) .".php", "w");
    $fp = fopen(PATH. strtolower($title) .".php", "w");
    $write = fwrite($fp, $content);

    fclose($fp);
    3 changes: 2 additions & 1 deletion MysqlTableMapper.php
    Original file line number Diff line number Diff line change
    @@ -5,12 +5,13 @@ class MysqlTableMapper{
    protected static $instance = null;
    protected $pdo;

    const HOST = 'localhost';
    const DATABASE = '';
    const USER = '';
    const PASS = '';

    public function __construct(){
    $this->pdo = new PDO(sprintf('mysql:host=%s;dbname=%s', 'localhost', DATABASE), USER, PASS);
    $this->pdo = new PDO(sprintf('mysql:host=%s;dbname=%s', HOST, DATABASE), USER, PASS);
    }

    public static function getInstance(){
    9 changes: 8 additions & 1 deletion gen.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,12 @@
    <?php

    $gen = new ModelGen();
    $gen->setDatabase('test');

    $gen->setHost('localhost');
    $gen->setUser('user');
    $gen->setPassword('pass');
    $gen->setDatabase('test');

    $gen->setPathToModel('path/to/models');

    $gen->generate();
  5. diguinhorocks revised this gist Jun 2, 2012. 3 changed files with 11 additions and 1 deletion.
    5 changes: 5 additions & 0 deletions ModelGen.php
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,11 @@ public function __construct(){
    public function setModel($model){
    $this->modelName = $model;
    }

    public function setDatabase($database){
    parent::DATABASE = $database;
    }


    protected function createModelHeader(){

    6 changes: 5 additions & 1 deletion MysqlTableMapper.php
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,12 @@ class MysqlTableMapper{
    protected static $instance = null;
    protected $pdo;

    const DATABASE = '';
    const USER = '';
    const PASS = '';

    public function __construct(){
    $this->pdo = Registry::getInstance()->get('connection');
    $this->pdo = new PDO(sprintf('mysql:host=%s;dbname=%s', 'localhost', DATABASE), USER, PASS);
    }

    public static function getInstance(){
    1 change: 1 addition & 0 deletions gen.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    <?php

    $gen = new ModelGen();
    $gen->setDatabase('test');
    $gen->generate();
  6. diguinhorocks created this gist Jun 2, 2012.
    141 changes: 141 additions & 0 deletions ModelGen.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,141 @@
    <?php

    class ModelGen extends MysqlMapper{

    protected $modelName;

    protected $model = null;

    protected $extends = ' extends MysqlActiveRecord';

    protected $implements = '';

    protected $mapper = null;

    public function __construct(){
    $this->mapper = parent::getInstance();
    }

    public function setModel($model){
    $this->modelName = $model;
    }

    protected function createModelHeader(){

    $this->model .= "<?php ". $this->printEndOfLine(2) ." class ".($this->tableToClassName($this->modelName)) . $this->extends . $this->implements . "{ " . $this->printEndOfLine(2) ;

    }
    protected function createModelFooter(){
    $this->model .= $this->printEndOfLine(1). " }". $this->printEndOfLine(2) . "?>";
    }

    protected function printEndOfLine($qtd){
    $lb = "";
    for($i = 0; $i < $qtd; $i++):
    $lb .= "\n";
    endfor;

    return $lb;
    }

    protected function printTab($size){
    $tab = "";
    for($i = 0; $i < $size; $i++):
    $tab .= "\t";
    endfor;

    return $tab;
    }


    public function generate(){
    foreach($this->mapper->showTables() as $table):

    $this->setModel($table);

    $this->createModelHeader();

    $this->getAttributes();

    $this->printEndOfLine(2);

    $this->getConstructor();

    $this->printEndOfLine(2);

    $this->getMagicSetter();

    $this->printEndOfLine(2);

    $this->getMagicGetter();

    $this->printEndOfLine(2);

    $this->createModelFooter();

    $this->writeInFile($this->modelName, $this->model);

    $this->model = "";
    endforeach;

    }

    protected function writeInFile($title, $content){
    $fp = fopen(MODEL. strtolower($title) .".php", "w");
    $write = fwrite($fp, $content);

    fclose($fp);
    }

    protected function getAttributes(){

    $columns = $this->mapper->mapColumns($this->modelName);
    foreach($columns as $data):
    $this->model .= $this->printTab(1)." protected $".$data['Field'].";" . $this->printEndOfLine(1);
    endforeach;
    }

    protected function tableToClassName($name){

    $isUnderLined = preg_split("/\_/", $name);
    $modelName = "";

    if($isUnderLined):
    foreach($isUnderLined as $pieces):

    $modelName .= ucfirst(strtolower($pieces));
    endforeach;
    else:
    $modelName = ucfirst(strtolower($name));
    endif;

    return $modelName;
    }

    protected function getConstructor(){
    $this->model .= <<<CONSTRUCTOR
    public function __construct() {
    parent::__construct(\$this);
    }
    CONSTRUCTOR;
    }

    protected function getMagicSetter(){
    $this->model .= <<<SETTER
    public function __set(\$name, \$value) {
    \$this->\$name = \$value;
    }
    SETTER;
    }

    protected function getMagicGetter(){
    $this->model .= <<<GETTER
    public function __get(\$name) {
    return \$this->\$name;
    }
    GETTER;
    }
    }
    45 changes: 45 additions & 0 deletions MysqlTableMapper.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <?php

    class MysqlTableMapper{

    protected static $instance = null;
    protected $pdo;

    public function __construct(){
    $this->pdo = Registry::getInstance()->get('connection');
    }

    public static function getInstance(){
    if(self::$instance == null):
    self::$instance = new MysqlTableMapper();
    return self::$instance;
    else:
    return self::$instance;
    endif;
    }

    public function mapColumns($table){

    $stm = $this->pdo->prepare('SHOW COLUMNS FROM '.$table.' FROM '.DATABASE);
    $stm->setFetchMode(PDO::FETCH_ASSOC);
    $stm->execute();

    return $stm->fetchAll();


    }

    public function showTables(){
    $stm = $this->pdo->prepare('SHOW TABLES FROM '.DATABASE);
    $stm->setFetchMode(PDO::FETCH_ASSOC);
    $stm->execute();
    $tables = array();
    foreach($stm->fetchAll() as $table):
    $tables[] = $table['Tables_in_'.DATABASE];
    endforeach;

    return $tables;
    }


    }
    4 changes: 4 additions & 0 deletions gen.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    <?php

    $gen = new ModelGen();
    $gen->generate();