Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Using the DAO Generator for PHP and MySQL : Page 3

Discover how to generate PHP classes that provide common MySQL database operations using the DAO design pattern.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Generating DAO Artifacts

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.

Figure 2. The Books Table Structure: This screen shows shows the structure and some sample data from the books table in the bookstore database.

To create the table in Figure 2, execute this SQL statement:

create table books (id int not null auto_increment primary key, title varchar(50), author varchar(50), yearofpublication int, publisher varchar(50), price int);

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", "Anghel Leonard",2009,"Packt",569);

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:

  1. In the ConnectionProperty class set the connection properties host, user, password, and database.
  2. Run the generated.php script.
  3. The output will look like this:
  4. generated/class/dto/Books.class.php varchar(50) varchar(50) varchar(50) varchar(50) int(11) int(11) varchar(50) varchar(50) int(11) int(11) generated/class/mysql/BooksMySqlDAO.class.php varchar(50) varchar(50) int(11) varchar(50) int(11) generated/class/dao/BooksDAO.class.php generated/include_dao.php generated/class/dao/DAOFactory.class.php

  5. 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:

<?php /** * Object represents table 'books' * * @author: http://phpdao.com * @date: 2009-08-10 22:50 */ class Books{ var $id; var $title; var $author; var $yearofpublication; var $publisher; var $price; } ?>

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).

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date