Search This Blog

Friday, April 29, 2011

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.

Read Full Details @ 

No comments:

Post a Comment