This section shows the process of generating a DAO artifact using a database named bookstore. The database contains a single table named books that has the structure shown in Figure 2.
To create the table in Figure 2, execute this SQL statement:
create table books
(id int not null auto_increment primary key,
Before generating the DAO artifacts, populate the books table with five records by executing the following INSERT statements:
insert into books values(1,"Annabel Lee",
"Edgar Allan Poe",1849,"The Literature Page",256);
insert into books values(2,"The Ballad of Reading Gaol",
"Oscar Wilde",1898,"The Literature Page",475);
insert into books values(3,"The Sonnets",
"Edgar Allan Poe",1602,"The Literature Page",300);
insert into books values(4,"Winnetow",
"Karl May",1956,"The truth",123);
insert into books values(5,"JBoss Tools 3",
With the database and books table created and populated, you can execute the DAO generator to create the DAO artifacts. To run the generator, follow these steps:
- In the ConnectionProperty class set the connection properties host, user, password, and database.
- Run the generated.php script.
- The output will look like this:
- The last three lines of the output show the names of generated classes, placed in a new generated folder that appears as a subfolder in the phpdao-1.7 directory.
The three classes generated for application work with the books table. The first class, in the file generated/class/dto/Books.php, defines an object that represents the books table:
* Object represents table 'books'
* @author: http://phpdao.com
* @date: 2009-08-10 22:50
The second class, in the generated/class/dao/BooksDAO.php file (see Listing 1), is an interface that defines various operations on the books table.
The third and final class, in the file generated/class/mysql/BooksMySqlDAO.php, implements the preceding interface class. You can see this class in the corresponding folder, (it's not included here because of its length).