Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.13 KB | None | 0 0
  1. <?php
  2.  
  3.     /**
  4.     *Ingenieria reversa de base de datos
  5.     *para crear un modelo de CodeIgniter sin esfuerzo.
  6.     *Uso : php script.php  <dbuser> <dbpass> <dbname> <table>
  7.     */
  8.  
  9.         if(!(isset($_SERVER["argv"][1]) && isset($_SERVER["argv"][2]) && isset($_SERVER["argv"][3]) &&isset($_SERVER["argv"][4]))){
  10.  
  11.                 echo "\nUsage :".basename($_SERVER["argv"][0]). " <dbuser> <dbpass> <dbname> <table>\n\n";
  12.                 exit(1);
  13.  
  14.         }
  15.  
  16.         $dbuser =       $_SERVER["argv"][1];
  17.         $dbpass =       $_SERVER["argv"][2];
  18.         $dbname =       $_SERVER["argv"][3];
  19.         $table  =       $_SERVER["argv"][4];
  20.  
  21.         $link           =       @mysql_connect("localhost",$dbuser,$dbpass);
  22.  
  23.         if(!$link){
  24.  
  25.                 echo "Couldnt connect to DB \"$dbname\"\n";
  26.                 exit (1);
  27.  
  28.         }
  29.  
  30.         mysql_select_db($dbname);
  31.  
  32.         $sql            =       "SELECT COLUMN_NAME FROM information_schema.columns WHERE TABLE_SCHEMA='$dbname' AND TABLE_NAME='$table'";
  33.         $result =       mysql_query($sql);
  34.  
  35.         if(!$result){
  36.  
  37.                 echo "Failed fetching \"$table\" columns\n";
  38.                 exit(1);
  39.  
  40.         }
  41.  
  42.         while($row      =       mysql_fetch_array($result,TRUE)){
  43.  
  44.                 $columns[]      =       $row["COLUMN_NAME"];
  45.  
  46.         }
  47.  
  48.         $code   =       "<?php\n\tclass ".ucwords($table)." extends CI_Model {\n\n";
  49.  
  50.         foreach($columns as $column){
  51.                 $code   .=      "\t\tvar\t$column\t=\tNULL\n";
  52.         }
  53.  
  54.         $code   .=      "\n\t\tvar\t".'$tableName'."\t=\t'".$table."';\n\n";
  55.  
  56.         $code.="\n\n\t\tpublic function __construct(){\n\n\t\t\tparent::__construct();\n\n\t\t}\n\n";
  57.  
  58.         foreach($columns as $column){
  59.  
  60.                 $colName        =       implode('',array_map("ucwords",explode('_',$column)));
  61.                 $code   .=      "\t\tpublic function set$colName".'($value=NULL)'." {\n";
  62.                 $code   .=      "\t\t\t".'$this->'.$column."\t=\t".'$value'.";\n";
  63.                 $code .= "\t\t}\n\n";
  64.  
  65.                 $code   .=      "\t\tpublic function get$colName".'($value=NULL)'." {\n";
  66.                 $code   .=      "\t\t\t".'return $this->'.$column.";\n";
  67.                 $code .= "\t\t}\n\n";
  68.  
  69.         }
  70.  
  71.  
  72.         $code.= "\t\tpublic function get(".'$last=0'.") {\n\n";
  73.         $code.= "\t\t\t".'if($last > 0){'."\n";
  74.         $code.= "\t\t\t\t".'return $this->db->get($this->_tableName,$last);'."\n";
  75.         $code.= "\t\t\t".'}'."\n\n";
  76.         $code.= "\t\t\t".'return $this->db->get($this->_tableName);'."\n";
  77.         $code.= "\t\t}\n\n";
  78.  
  79.         $code.= "\t\tpublic function insert() {\n";
  80.         $code.= "\t\t\t".'$this->db->insert($this->_tableName, $this);'."\n";
  81.         $code.= "\t\t}\n\n";
  82.  
  83.         $code.= "\t\tpublic function update() {\n";
  84.         $code.= "\t\t\t".'$this->db->update($this->_tableName,$this,array('."'id'=>".'$this->id));'."\n";
  85.         $code.= "\t\t}\n\n";
  86.  
  87.         $code.="\t}\n";
  88.         $code.="?>";
  89.  
  90.         $table  =       ucwords($table);
  91.  
  92.         echo "Saved to $table.model.php!\n";
  93.  
  94.         file_put_contents("$table.model.php",$code);
  95.  
  96. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement