PHP Classes

Entity Generator: Generate database table access classes

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 68%Total: 203 All time: 8,457 This week: 38Up
Version License PHP version Categories
entitygenerator 1.0.0The PHP License5PHP 5, Databases, Code Generation
Description 

Author

This package can generate database table access classes.

It provides classes to retrieve information about database tables and fields.

Separate scripts will generate classes with getter and setter functions to access the listed database tables within there own namespace.

Picture of Channaveer Hakari
  Performance   Level  
Innovation award
Innovation award
Nominee: 2x

 

Example

<?php
require_once 'init.php';
$directoryName = 'Entity';
// error_reporting(E_ALL);
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
ini_set('max_execution_time', 0);

if(
$_SERVER['REQUEST_METHOD'] == 'POST'){
   
$databaseRepository = new EntityGenerator\Database\DatabaseRepository($connection);
   
$database = filter_input(INPUT_POST,'database');
   
$tables = explode(',',filter_input(INPUT_POST,'tables'));
    if(empty(
$databases) == true && isset($tables[0]) && $tables[0] == ''){
       
header("Location:index.php?error=Select database and tables");
        return
false;
    }
   
$connection->exec("USE $database");

    foreach(
$tables as $table){
       
$coloumnNames = $databaseRepository->getColoumnNamesOfTable($table);
       
generateEntityClass($table, $coloumnNames, $database);
    }
   
header("Location:index.php?success=Successfully Generated Entities");
}else{
   
header("Location:index.php?error=Oops! Error in generating entities.");
}

function
generateEntityClass($tableName, $coloumnNames, $database){
    global
$directoryName;
   
$studlyTableName = EntityGenerator\Helper\HelperFunctions::studlyCaps($tableName);
   
$entityClass =
"
<?php
namespace
{$directoryName};\n
class
{$studlyTableName}{
   
"
;
   
    foreach(
$coloumnNames as $coloumnName){
       
$entityClass .= " private \$".EntityGenerator\Helper\HelperFunctions::camelCase($coloumnName['Field']).";\n";
        if(
$coloumnName['Key'] == 'PRI' || $coloumnName['Key'] == 'PRIMARY'){
           
$entityClass .= getGetter($coloumnName['Field']);
            continue;
        }else{
           
$entityClass .= getGetter($coloumnName['Field']);
           
$entityClass .= getSetter($coloumnName['Field']);
        }
    }
$entityClass .= "}";
   
writeFile($database,$studlyTableName.'.php', $entityClass);
}

function
getGetter($coloumnName){
   
$studlyColoumnName = EntityGenerator\Helper\HelperFunctions::studlyCaps($coloumnName);
   
$camelColoumnName = EntityGenerator\Helper\HelperFunctions::camelCase($coloumnName);
   
$getter =
"
    public function get
{$studlyColoumnName}(){
        return \$this->
{$camelColoumnName};
    }
\n"
;
    return
$getter;
}

function
getSetter($coloumnName){
   
$studlyColoumnName = EntityGenerator\Helper\HelperFunctions::studlyCaps($coloumnName);
   
$camelColoumnName = EntityGenerator\Helper\HelperFunctions::camelCase($coloumnName);
   
$setter =
"
    public function set
{$studlyColoumnName}(\${$camelColoumnName}){
        \$this->
{$camelColoumnName} = \${$camelColoumnName};
        return \$this;
    }
\n"
;
    return
$setter;
}

function
writeFile($database,$fileName, $content){
    global
$directoryName;
   
$database = EntityGenerator\Helper\HelperFunctions::studlyCaps($database);
    if (!
file_exists(__DIR__."/{$directoryName}/{$database}/")) {
       
mkdir(__DIR__."/{$directoryName}/{$database}/", 0777, true);
    }
    if(!
defined('FILE_WRITE_PATH')){
       
define('FILE_WRITE_PATH', __DIR__."/{$directoryName}/{$database}/");
    }
    if(
$fh = fopen(FILE_WRITE_PATH.$fileName,'w+')){
        if(
is_writable(FILE_WRITE_PATH.$fileName)){
           
fwrite($fh, $content);
        }else{
            exit(
'Please provide Read and Write permissions for directory');
        }
    }else{
        exit(
'Please provide Read and Write permissions for directory');
    }
}


Details

EntityGenerator

Entity Generator is used to generate entity classes for partial (specific) or all tables of existing database.

NOTE : As of now tested for only MySQL database.

Installation Instructions

Step 1 : Download the zip file.

Step 2 : Upload in the www folder or htdocs folder where you will run your php projects

Step 3 : In browser open the respective project and browse for database and respective tables.

Step 4 : Generated classes will be stored in project root folder "Entity" with datbase name folder within it.

NOTE : Dont forget to change the namespace of your files

In case your bored to change the namespace in all the files then better go for 'generateEntities.php" file and change the name of variable

$directoryName = 'Entity';

Happy Coding!


  Files folder image Files (27)  
File Role Description
Files folder imageEntityGenerator (5 directories)
Accessible without login Plain text file autoload.php Aux. Auxiliary script
Accessible without login Plain text file generateEntities.php Example Example script
Accessible without login Plain text file getTables.php Example Example script
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file init.php Example Example script
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:203
This week:0
All time:8,457
This week:38Up
User Ratings User Comments (1)
 All time
Utility:85%StarStarStarStarStar
Consistency:85%StarStarStarStarStar
Documentation:85%StarStarStarStarStar
Examples:85%StarStarStarStarStar
Tests:-
Videos:-
Overall:68%StarStarStarStar
Rank:433
 
nice
8 years ago (muabshir)
80%StarStarStarStarStar