See our new project php tag library

DAO generator for PHP and Mysql

Data Access Object: design pattern, access to data varies depending on the source of the data. Access to persistent storage, such as to a database, varies greatly depending on the type of storage (relational databases, object-oriented databases, flat files, and so forth) and the vendor implementation.
This DAO generator generate php classes which provide operations on Mysql database (remember if you want use transactions use innodb engine).
Please use for each table primary key and this column type must be int. Dao generator doesn't generate classes for table which doesn't containt primary key.
In general it is not recommended to have table without primary key.

How to generate classes
- Download latest version of DAO generator for PHP and Mysql
- unzip file
- set connection to database properties in file templates/class/dao/sql/ConnectionProperty.class.php
- run file generate.php (for example: http://localhost/phpdao/generate.php)
- generated classes will put in 'generated' folder

How does it work
Application generate three classes for one table (table must contain primary key). First class is Domain Transfer Object, example
Second is interface that define operations on table example
Third class implements preceding interface example
Now you can write your non standard queries whith where not generated, for example from two tables. Here is an example

How to write non standard queries:
In directory: generated\class\mysql\ext this tool generates empty classes. In that classes you can write your methods likes in example. DAOFactory now return that classes instances. So when you call DAOFactory::getContentDAO() you have all methods from class ContentMySqlDAO and ContentMySqlExtDAO. When you write methods in ContentMySqlExtDAO and you will regenerate classes, this file will not be override. Download examples and open file ContentMySqlExtDAO.class.php to see how you can write new sql queries.

- All calls through DAOFactory,

PHP 5x

Questions, suggestions
email me at: info (at)


1.0 - first version

1.1 - adding possibility to get generated id of inserted row

1.2 - adding method 'clean', fixed problem in 'queryAll' method

1.3 - protecting SQL Injection

1.4 - fixed problem with values that contained char '?'

1.5 - fixed problem with null values

1.6 - fixed problem with null values

1.7 - additional methods

1.8 - changed sql query in method clean: truncate table - > delete from
       If table ends with 's' letter, then domain transfer object does not contain this letter
       Solved problem in method update when priomary key name was not equal 'id'

2.0 - Now you can write your non standard queries whith where not generated, for example from two tables. Here is an example

2.1 - Complex primary key support

2.2 - Support for create row with null field

2.3 - Remove warnings and assigning generated PK to valid field in DTO

2.4 - I made order with comments in classes

2.5 - properties of DTO classes does not contain letter 's', when columne name ended with 's'

2.6 - little problem with numerics

2.6 - problem with null values resolved (author: Brighton Kukasira)

You can download examples. This archive contain sample sql script which create tables, generated classes and file index.php which demonstrate uses phpdao.

More examples

Tags: php Data Access Object, DAO generator for PHP and Mysql. DAOGenerator, DAO generator for PHP and Mysql, Data Access Object, php mysql object generator

See also:
Hibernate vs jdbc
Service locator for EJB

tomcat monitoring
Mysql vs Postgres vs Oracle vs Mssql