PHP Classes

MySQL Class Generator: Generate classes to access MySQL as objects

Recommend this page to a friend!
  Info   Example   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStarStar 80%Total: 1,061 This week: 3All time: 3,495 This week: 12Up
Version License PHP version Categories
mysqlreflection 1.0.11BSD License5PHP 5, Databases, Code Generation
Description 

Author

This package can perform automatic generation of PHP classes from MySQL tables.

It is able to access to a given MySQL database and retrieve schema tables and fields.

For each table it generates a class that provides methods for accessing, reading and writing its records by using object oriented programming logic.

Each generated PHP class provides Setter and Getter methods for all table fields, as well as methods for CRUD operations.

Picture of Saro Carvello
  Performance   Level  
Name: Saro Carvello <contact>
Classes: 2 packages by
Country: Italy Italy
Age: 57
All time rank: 141958 in Italy Italy
Week rank: 16 Up1 in Italy Italy Up

Recommendations

Map database records to objects
Store and retrieve objects without writing native SQL again

What is the best PHP crud class?
How to create CRUD with PHP

doing all crud operations
doing all crud operations with one class

What is the best PHP crud class?
Manage and display staff details from database

database crud for each table
Package to analyze each table and create pdo crud for each

mysql database
script for update mysql database

How to find package ?
I'd like find package for accessing MySQL database.

php class file for InnoDB having the transaction handling
Mysql Innodb connection handling in PHP

PHPeclipse - PHP - Code Templates
Generate DB class, manager and exception file like PHPeclipse

MySQL table editor
Need to be able to create an interface to edit the table data

Good and simple MySQL wrapper
What MySQL wrapper should I go with?

Best PHP mysql to mysqli or PDO solution
How to convert mysql code to use mysqli?

Access data base
Pre-written PHP MySQL functions class

Example

<?php
include_once("mysqlreflection.config.php");
define("DESTINATION_PATH",dirname(__FILE__) . "/beans/");
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
    <title>MySQL Database Bean Builder</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- Bootstrap core CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" media="screen">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js"></script>
    <![endif]-->
    <style>
        .progress {
                background: rgba(204, 237, 220, 1);
                border: 5px solid rgba(56, 46, 166, 0.27);
                border-radius: 10px; height: 36px;
        }
    </style>
<script>
    var globalCount =0 ;
    var globalPercent = 0;
</script>
<body>
<div class="container">
    <h1>MySQL Database Beans generator</h1>
    <h3>This utility performs automatically a source code generation of PHP
        Classes from MySQL tables </h3>
    <h4>Current database :<?= DBNAME ?> (to change it edit mysqlreflection.config.php)</h4>
    <h4>Destination path :<?= DESTINATION_PATH ?></h4>

    <a class="btn btn-success" onclick="document.getElementById('results').value = ''" href="?build=1"><span class="glyphicon glyphicon-wrench"></span> Generate classes</a>
    <a href="../builders/index" class="btn btn-info"><span class="glyphicon glyphicon-home"></span> Home</a>
    <br /> <br />
    <div class="progress progress-striped">
        <div class="progress-bar" role="progressbar" aria-valuenow="0"
             aria-valuemin="0" aria-valuemax="100" style="width:0%">
        </div>
    </div>

    <div class="text-center">
        <textarea cols="140" rows="20" id="results" name "results"></textarea>
    </div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>

<script>
    function aggiornaProgressBar(done=false) {
        var step = 2;
        var progress = $('.progress-bar');
        var currentValue = parseInt(progress.attr("aria-valuenow"));
        globalCount = globalCount +1;
        globalPercent = globalPercent +1;
        if (currentValue==100) {
            currentValue = 50;
        }
        currentValue += 1;
        progress.attr("aria-valuenow",currentValue);
        var percValue = currentValue + '%';
        progress.css('width',percValue);

        var textarea = document.getElementById('results');
        textarea.scrollTop = textarea.scrollHeight;

        if (done) {
            progress.attr("aria-valuenow",100);
            progress.css('width',"100%");
            percValue = "Done. " + globalCount + " classes were generated";
        }
        progress.html(percValue);
    }

    function aggiornaTextArea(msg){
        var m = msg + '&#xA;';
        $('#results').append(m);

    }

    function setNumberOfTables(ntables){
        if (numberOfTables === 'undefined' || !numberOfTables)
            var numberOfTables=ntables;
    }
</script>

</body>

<?php
if (isset($_GET["build"])) {
   
/**
     * Demo application: generate classes from a mysql db schema
     */

    // CLI mode
    // error_reporting(E_ALL);
    // include_once("mysqlreflection/mysqlreflection.config.php");
    // header('Content-Type: text/html; charset=utf-8');

   
$msg = "Building classes for mysql schema:[" . DBNAME . "]";
   
// CLI mode
    // echo $msg;
   
echo "<script>$('#results').append('" . $msg . "');</script>";

   
// Destination path for the generated classes
   
$destinationPath = DESTINATION_PATH;
   
// $destinationPath = "source/";

    // Create reflection object and invoke classes generation from the specified schema into mysql_connection.inc.php
   
$reflection = new MVCMySqlSchemaReflection();

   
// Generates the classes into the given path. During the generation it outputs the results.
   
$reflection->generateClassesFromSchema($destinationPath);

   
// CLI Mode
    // echo "<hr>Done.";
    // echo "<script> window.scrollTo(0,document.body.scrollHeight);</script>";

   
echo "<script>$('#results').append('" . "Done." . "&#xA;" . "');</script>";
    echo
"<script>aggiornaProgressBar(true);</script>";
}
?>


Details

MySQLReflection

A useful utility for the Object Relation Mapping of MySQL databases

The utility generates automatically PHP classes for any tables of a given database schema.

Package is extracted from my personal PHP Web MVC Framework.

To generate PHP classes with the purpose of mapping database tables you need to:

1) IMPORTANT! Use lowercase with the underscore, which is the widely used MySQL naming notation, on your database tables and fields names.

2) Configure your database schema by modifying util\mysqlreflection\mysqlreflection.config.php file and, assigning appropriate values to DBHOST, DBNAME, DBUSER, DBPASSWORD, and DBPORT PHP constants, according to your MySQL settings.

3) Then, launch the tool by typing: http/localhost/util/app_create_beans.php

Note that, the GUI of the utility uses Bootstrap and jQuery from CDN. So you also need an internet connection alive before running it.   

4) Once the utility is started, click "_Generate classes_" button.

After running the utility you will find all generated classes into "beans" folder.

Note:

  • Auto generated classes are prefixed with "Bean".
  • Each auto generated class uses bean\bean.config.php for configuration info.
  • Each auto generated class uses Model.php and MySqlRecord.php

An auto generated MySQL class provides the following services:

  • A constructor for managing a fetched table?s row or for a adding a new one
  • Management for both single or composite Primary Keys
  • Automatic mapping of the different date formats may occurs between application and database
  • Destructor to automatically close database connection
  • Defines a set of attributes corresponding to the table fields
  • Setter and Getter methods for each attribute
  • OO methods for simplify DML select, insert, update and delete operations
  • A facility for quickly updating a previously fetched row
  • Useful methods to obtain table DDL and the last executed SQL statement
  • Error handling of SQL statements
  • Camel/Pascal case naming convention for Attributes/Class used for mapping Fields/Table
  • Useful PHPDOC information about table, fields and the usage of class, attributes and methods.

Developers can quickly use these classes on their application.

Into the beans directory are located some auto generated classes of a MySQL schema.

DDL for MySQL schema is into a script file located into "sql" directory.

There is also a simple tester script "app_run_bean.php" for showing usage of a generated class.

Utility GUI

alt tag

Overview

alt tag

UML Class Model

alt tag

Convert MySQL into PHP Classes automatically - Video Tutorial (old user interface)

IMAGE ALT TEXT HERE

Thanks

Many and many thanks to https://www.jetbrains.com for granting me an open source license of magic

https://www.jetbrains.com/phpstorm/ PHPStorm


that I used in the development of MySQLReflection.


Screenshots (3)  
  • ClassModel.png
  • gui
  • how_it_works
Videos (1)  
  • Convert MySQL into PHP Classes automatically
  Files folder image Files (40)  
File Role Description
Files folder imagebeans (17 files)
Files folder imagedocs (6 files)
Files folder imagemysqlreflection (7 files)
Files folder imagesql (1 file)
Accessible without login Plain text file app_create_beans.php Example Example script
Accessible without login Plain text file app_run_bean.php Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file mysqlreflection.config.php Aux. Auxiliary script
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (40)  /  beans  
File Role Description
  Accessible without login Plain text file bean.config.php Aux. Auxiliary script
  Plain text file BeanAclActions.php Class Class source
  Plain text file BeanDepartments.php Class Class source
  Plain text file BeanDeptEmp.php Class Class source
  Plain text file BeanDeptManager.php Class Class source
  Plain text file BeanEmployees.php Class Class source
  Plain text file BeanMultiplePk.php Class Class source
  Plain text file BeanSalaries.php Class Class source
  Plain text file BeanSinglePkDate.php Class Class source
  Plain text file BeanSinglePkDateTime.php Class Class source
  Plain text file BeanSinglePkInt.php Class Class source
  Plain text file BeanSinglePkIntAuto.php Class Class source
  Plain text file BeanSinglePkString.php Class Class source
  Plain text file BeanTitles.php Class Class source
  Plain text file BeanUser.php Class Class source
  Plain text file Model.php Class Class source
  Plain text file MySqlRecord.php Class Class source

  Files folder image Files (40)  /  docs  
File Role Description
  Accessible without login Image file gui.png Data Auxiliary data
  Accessible without login HTML file index.html Doc. Documentation
  Accessible without login Image file MySQLReflection.png Data Auxiliary data
  Accessible without login Plain text file README.md Doc. Documentation
  Accessible without login Plain text file tutorial_text.txt Doc. Documentation
  Accessible without login Image file UMLClassModel.png Data Auxiliary data

  Files folder image Files (40)  /  mysqlreflection  
File Role Description
  Plain text file MVCMySqlBeanBuilder.php Class Class source
  Plain text file MVCMySqlBeanClass.php.tpl Class Class source
  Plain text file MVCMySqlFieldToAttributeReflection.php Class Class source
  Plain text file MVCMySqlPKAnalyzer.php Class Class source
  Plain text file MVCMySqlSchemaReflection.php Class Class source
  Plain text file MVCMySqlSimpleTemplate.php Class Class source
  Plain text file MVCMySqlTableReflection.php Class Class source

  Files folder image Files (40)  /  sql  
File Role Description
  Accessible without login Plain text file bean_db.sql Data Auxiliary data

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  
 92%
Total:1,061
This week:3
All time:3,495
This week:12Up
User Ratings User Comments (1)
 All time
Utility:91%StarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:91%StarStarStarStarStar
Examples:91%StarStarStarStarStar
Tests:-
Videos:75%StarStarStarStar
Overall:80%StarStarStarStarStar
Rank:13
 
Very useful to autogenerate PHP classes from MySQL.
7 years ago (Rossella Aiello)
87%StarStarStarStarStar