PHP DAO Builder v3.2


Data Access Objects Builder on GitHub

This is simple web tool for automatic creation of DAO persistance layer classes from existing MySQL database schema.


Files organization

DataModel for which DAO layer will be built File tree after PHP DAO build for some tables
Database Model File tree after PHP DAO

Database connection

Put PHPDAO app folder inside web root folder of your web app on local server (make 'models' folder writable for PHPDAO) and start PHPDAO:

http://localhost/yourwebapp/PHPDAO/index.php

PHPDAO - connection

Database tables/views

The list of all tables from previously given db schema will be shown

PHPDAO - tables/views

Select all tables you want to build DAO classes for. DAO PHP will try to guess name (singular and plural words) for each table/DAO class but don't rely on its english grammar capabilities :-). Put your own correct or other wanted names if necessary. For example, if you inside database have one-to-many relation between 'employees' and 'projects' tables, and for 'employees' table define 'Employee' (singular) and 'Employees' (plural) names and for 'projects' table define 'Project' (singular) and 'Projects' (plural) names, then PHP DAO will generate Employee class for 'employees' table and getProjects() function in it for retrieving related (many) objects from related table 'projects' and vice versa getEmployee() function for retrieving related (one) object inside Project class.

NOTE: Table comment (comment from database) will be used as initial description of class inside DAO code. PHP DAO uses CamelCase naming for classes (underscores are onitted), so if table name inside database is 'employees_projects', PHP DAO will guess that class name should be EmployeeProject (singular) and EmployeeProjects (plural)


Tables/views columns

Previously selected tables are shown (as web accordion) with their columns and column attributes and relations to other tables. One can select here what columns should be included in DAO class build process. By default all columns are selected but you can exclude some of them if you want. (Primary key column(s) are selected and mandatory.) For example if column 'name' is selected, then class Employee will have public property name ($Employee->name). The similar thing is with finder functions, if you want particular finder functions for some columns, select them in finder column (columns that are indexed inside database are selected by default because PHP DAO assumes you want finder functions for them). So if finder is selected for column name, PHP DAO will create findByName($name) function (always prefixed with findBy). Here you defined creation of particular finder functions but, ff course, each DAO class will have generic 'find()' function you can use for searching/finding records by any column (columns given as associative array).

NOTE: Column comment (comment from database) will be used as initial description of property inside DAO code. PHP DAO uses CamelCase naming for properties (underscores are onitted), so if column name inside database is 'first_name', PHP will create finder function with name findByFirstName($firstname)

PHPDAO - columns

DAO builder

PHP DAO generator starts and finally shows what classes have been created. (One DAO class for each selected table, plus DA.php and DAO.php core classes inside models/DAO/ subfolder and one table class inside models for customization). PHPDAO folder can be deleted if you do not intend to rebuild DAO layer for tables in database. But after every database model change one should rebuild DAO layer to reflect changes made in tables/columns. Only models/DAO/*.dao.php classes of DAO layer are overwritten (but backuped before).

PHPDAO - builder

Functions

PHP DAO can build DAO classes for both; single and composite primary key tables. (Single primary key (PK) table has only one column as primary key and it can be auto-incremented (AI). Composite primary key table has primary key which is combination of more columns and in this case auto-increment is not possible - primary keys should be defined explicitelly.)

So CRUD(S) ('Create', 'Read', 'Update', 'Delete', and 'Save') functions accept as parameter single primary key value or associative array of primary key names => values:

Create

Example #1: (Single PK - auto-incremented)

        require 'models/Product.php';

        try
        {
          $Product=new Product();
          $Product->name='Car';
          $Product->create(); //PK is auto-incremented
          $id=$Product->getId();// return autoincremented PK
        }
        catch(CreateException $e)
        {
          //handle exception
        }
      
Example #2a: (Composite PK - set as properties of Ids):

        require 'models/Usage.php';

        try
        {
          $Usage=new Usage();
          $Usage->setProductId(1); // Primary key column #1
          $Usage->setServiceId(2); // Primary key column #2
          $Usage->setDateFrom(date('Y-m-d');
          $Usage->create();
          $ids=$Usage->getIds(); //array of ids
        }
        catch(CreateException $e)
        {
          //handle exception
        }
      
Example #2b: (Composite PK - set inside associative array):

        require 'models/Usage.php';

        try
        {
          $Usage=new Usage();
          $Usage->setDateFrom(date('Y-m-d');
          $Usage->create(['productId'=>1,'serviceId'=>2]);
        }
        catch(CreateException $e)
        {
          //handle exception
        }
      

Read

Example #1a: (id given in read() function)

        require 'models/Service.php';

        try
        {
          $Service=new Service();
          $Service->setId(1);
          $Service->read();
        }
        catch(ReadException $e)
        {
          //handle exception
        }
      
Example #1b: (id given in constructor)

        require 'models/Product.php';

        try
        {
          $Product=new Product(1);
        }
        catch(ReadException $e)
        {
          //handle exception
        }
      

Update

Example #1:

        require 'models/Product.php';

        try
        {
          $Product=new Product(100); //create object and read Product with id=100
          $Product->name='Truck'; //change name of existing Product
          $Product->update(); //update new data
        }
        catch(UpdateException $e)
        {
          //handle exception
        }
      

Delete

Example #1a:

        require 'models/Service.php';

        try
        {
          $Service=new Service();
          $Service->delete(1);
        }
        catch(DeleteException $e)
        {
          //handle exception
        }
      
Example #1b:

        require 'models/Service.php';

        try
        {
          $Service=new Service(1);
          $Service->delete();
        }
        catch(DeleteException $e)
        {
          //handle exception
        }
      

Save

Save function insertes record but if record already exist or if any of referential constraints on this table is violated (unique key or referential constraint is violated) then existing record is updated.


        require 'models/Service.php';

        try
        {
          $Service=new Service(1);
          $Service->name='Truck';
          $Service->save();
        }
        catch(DeleteException $e)
        {
          //handle exception
        }
      

Specific finders

Example #1: (specific findByName() finder created by PHP DAO)

        require 'models/Service.php';

        $Product=new Product();
        $Products=$Product->findByName('Car');
        foreach($Products as $Product)
        {
          echo $Product->name.PHP_EOL;
        }
      

Generic finder

Example #1: (argument is associative array where keys are table column names)

        require 'models/Customer.php';

        $Customer=new Customer();
        $Customers=$Customer->find(['name'=>'Johnny','surname'=>'Depp']);
        foreach($Customer as $Customer)
        {
          echo $Customer->surname.', '.$Customer->name;
        }