Skip to content

MySQL ORM

Rosario Carvello edited this page Feb 18, 2018 · 15 revisions

Introduction

WebMVC provides you with a useful utility for the Object Relation Mapping of MySQL. The utility generates automatically Model classes for any tables of a given database schema.

WebMVC Model and ORM - Object Relational Mapping

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

  1. Using lowercase an underscore, which are 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 utility 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.

The following figure shows you the startup screen of the utility:

Utility GUI

alt tag

After running the generation of classes you can close the utility. You will find all the generated classes under models\beans folder.

Notice that:

  • You can find a class for each table of your MySQL schema.
  • Each auto-generated class name is prefixed with "Bean" followed by the table name in a PascalCase format. E.g, for table name users_roles you will find a class named BeanUsersRoles.
  • Each auto-generated class extends framework\Model.php. Consequently, it is itself a Model you can relate to a Controller

Each auto-generated Model class provides you with the following services:

  • A constructor for managing a fetched table’s row or for adding a new one
  • Management for both single or composite Primary Keys
  • Automatic mapping of the different date formats may occur 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 simplifying the DML operations of SELECT, INSERT, UPDATE, and DELETE.
  • 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, class attributes and usage of methods.

// TODO

Clone this wiki locally