Getting started with PHP Class Generator Plugin

The Eclipse plugin PHP Class Generator is now about a month up on Eclipse Plugin Central. It was now brought to my attention (thanks Tyler) that documentation is not exactly missing but at least hard to find and you have to know where and how to look. This article should help to shed light into the dark.

Tyler Anderson wrote an article based on this plugin.
There are different aspects to the use of the plugin. If you are stuck with the plugin at some point feel free to jump ahead. First I start with describing the basis on which the plugin works. The last chapter then is a walk through the plugin.

Motivation
PHP based web applications often use a database as data source. PHP is a programing language that requires disciplined programming skills of the programmer; meaning you can fast and easily produce code that does what you want. Especially in greater projects this has its drawbacks: It is not easy to maintain, no re-usability, code duplication, … OO programming is a solution to these problems. PHP provides the possibility to make use of this concept (especially since version 5). Now you can define the objects (business objects) based on the tables on the database or on the displayed data. What is the better solution is for you and your project to decide. With an eye on creating business objects I designed my data structures in such a way that it is easy to en-capsule the data from the database into objects. The basic idea is to generate Bean with getter and setter methods and some additional methods for creating instances of this object e.g. by loading from the database. To gain greater flexibility over what is created the creation is based on an XML template that defines the “what”.
Installation
First you have to download the plugin from the project page or what is probably closer to your home Sourceforge. Then you have to unzip the plugin with a product of your choice. The zip archive is named “ch.sahits.phpclassgenerator_”version. The archive itself contains a folder with the same name. Depending on how you unzipped the archive you will get both folders or only the inner one. As a matter of fact only the inner is of importance. Take this folder and copy it into the “plugins” directory of your eclipse installation. Start/Restart your eclipse. Installation complete. The picture below shows the directory listing of the plugin.
phpclassgenerator plugin
Database
As seen above the database is a central part of the generation of the resulting PHP class. Though you can use the generator without any DB the full potential comes in with the database. Up until now only MySQL is supported. The neat thing is that the generator can produce methods to read from the DB by primary key or foreign key. To get an idea of how such tables can look here is an example I will use throughout this tutorial. There are two tables: User and Usergroup. Each table has a numeric id primary key. Each user belongs to a usergroup. Therefor there is a constraint on the user table that indicates that the usergroup_id field references the id value of the table usergroup. This constraint constitutes a foreign key. So here are the create statements for the two tables:
CREATE TABLE `user` (
`ID` int(11) NOT NULL auto_increment,
`USERGROUP_ID` int(11) NOT NULL,
`USER` varchar(20) NOT NULL,
`PASSWD` varchar(1024) NOT NULL,
`EMAIL` varchar(60) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `USERGROUP_ID_2` (`USERGROUP_ID`,`USER`),
KEY `USERGROUP_ID` (`USERGROUP_ID`)
)

Because the usergroup_id is a foreign we define an index over it. Further we state that any user(name) must be unique in any usergroup. This is ensured by the unique key.
CREATE TABLE `usergroup` (
`ID` int(11) NOT NULL auto_increment,
`NAME` varchar(40) NOT NULL,
`GRUPPE_C` int(11) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `NAME` (`NAME`)
)

The usergroup table is simpler. Any group name must be unique. Now we can add the foreign key constraint:
ALTER TABLE `user`
ADD CONSTRAINT `user_ibfk_1` FOREIGN KEY (`USERGROUP_ID`) REFERENCES `usergroup` (`ID`) ON UPDATE CASCADE

XML-Template
You can use an XML template that produces always the same output invariable of the chosen database table. In the template you can define methods and their implementation in PHP. But this contradicts the effort of OO programing: such method should be extracted into a super class and made available to all extending classes. So now you might ask what does the template then offer? The answer is template Strings. These are strings the the wizard provides a replacement. What they are and what they do we will see further on.
Lets first have a look at the structure of the xml file. Therefore we study its schema definition that can also be downloaded from the project page. For better clarity here is graphical representation of the structure:
php class generator xsd
The tag include contains a String. Each string denotes an include file. Further there are the tags member and members. Both define member variables of the class. member defines a single variable where as members is a template string and is replaced by several variables. Both tags have the attributes name (required) defining the name of the variable and visibility stating just that for the variable. The attribute value of member holds its value. members has the attribute initPK that takes a boolean and indicates if the primary key should be initialized.
On the same level are the tags for method and methods that work the same way as member and members. Their attributes are name (required), static and visibility. The details of the method(s) are encapsuled in inner tags: The arguments or parameter can be supplied as defined list of arguments or as a template string. Then there is the optional description of the method. An last but not least the body that holds the implementation details of the method. For further details please check the schema for your self.
When we look at the provided example xml template we see two includes: Security.php is never used. MySQLDAO.php is my class for DB access. The variable $dao is of type MySQLDao. When you create a class from this template you will discover, that the $dao is not initialized in the constructor. Because the class MySQLDao is not provided with the example you have to do something on your own.
Template-Strings
The template strings are a fundamental concept of the class generation. Therefore I will list them here:

  • DB_FIELDS: can be used with the tag members: member variables for all DB fields are generated
  • DB_FIELD_GETTER: can be used with the tag methods: getter methods for the DB fields are generated
  • DB_FIELD_SETTER: can be used with the tag methods: setter methods for the DB fields are generated
  • LOAD_BY_FK: can be used with the tag methods: load methods for all foreign keys are generated. Method body fragment needed.
  • LOAD_UNIQUE: can be used with the tag method: load method for the primary keys are generated. Method body fragment needed.
  • TO_STRING: can be used with the tag method: convert the data read from the DB into a formatted string.
  • INIT_POST: can be used with the tag method: initialize the object by values found in the $_POST array
  • INIT_GET: can be used with the tag method: initialize the object by values found in the $_GET array
  • INIT_LOAD: can be used with the tag method: initialize the object by values found in the argument array
  • INSERT: can be used with the tag method: insert method is generated. Method body fragment needed.
  • UPDATE: can be used with the tag method: update method is generated. Method body fragment needed.
  • _WHERE: can be used in the tag body in combination with PK or FK.
  • PK: can be used in the tag body in combination with _WHERE to generate the where clause using the primary key
  • FK: can be used in the tag body in combination with _WHERE to generate the where clause using the foreign keys
  • CLASS_NAME: class name derived from the table name (lower case with capital)
  • OBJECT_NAME: object name derived from the table name (lower case)
  • FIELD_ARRAY: string to initialize a PHP array with all DB fields.
  • DB_NAME: name of the database supplied in the wizzard
  • DB_HOST: host of the database supplied in the wizzard
  • DB_USER: user of the database supplied in the wizzard
  • DB_PASSWD: password of the user supplied in the wizzard

Using the plugin
After all that dry technical stuff lets get cracking and see how the plugin works.
Before you start the wizard you should select your project and place the selection on the output folder for the class to bee. Then you start the wizard by selecting ‘File/New/Other/PHP/New DB PHP class’. If you have both PHPEclipse and PDT installed you will see two PHP folder.
PHP class generator wizard Step 1
The next button brings you to the first of the two wizard pages. Here you state the location where the file should be generated into. Note that here is the location you placed your selection earlier. Then you have to name your new class. Finally choose a xml template by browsing for it.
PHP class generator wizard Step 2
The next button brings you to the last page. Here you must select your database information. The only database supported is MySQL so you have no real choice there. Since you are a serious developer you will have an instance of the database at your localhost or else specify the path to the host. Then you have to name the database. You must also provide a username and a password for the db. Since your database is local you will have root access. Lastly you must specify the tablename for which you wish to create the php class.
PHP class generator wizard Step 3
If all data is correct an processable by hitting the finish button the new file will be created and displayed.
In the case of our example of the user table this is the result:
< ?php
include 'MySQLDAO.php';
include 'Security.php';
class User {
private $dao;
private $i = 1;
private $id = 0;
private $usergroupId;
private $user;
private $passwd;
/**
* Retrieves the value from the field ID
* @return String with the value of the field
*/
public function getId(){
return $this->id;
}
/**
* Retrieves the value from the field USERGROUP_ID
* @return String with the value of the field
*/
public function getUsergroupId(){
return $this->usergroupId;
}
/**
* Retrieves the value from the field USER
* @return String with the value of the field
*/
public function getUser(){
return $this->user;
}
/**
* Retrieves the value from the field PASSWD
* @return String with the value of the field
*/
public function getPasswd(){
return $this->passwd;
}
/**
* Set the value from the field ID
* @param id String with the value for the field
*/
public function setId($id){
$this->id = $id;
}
/**
* Set the value from the field USERGROUP_ID
* @param usergroupId String with the value for the field
*/
public function setUsergroupId($usergroupId){
$this->usergroupId = $usergroupId;
}
/**
* Set the value from the field USER
* @param user String with the value for the field
*/
public function setUser($user){
$this->user = $user;
}
/**
* Set the value from the field PASSWD
* @param passwd String with the value for the field
*/
public function setPasswd($passwd){
$this->passwd = $passwd;
}
/**
* Default constructor
* @param value some value
*/
function __construct($value){
}
/**
* Initialize the business object with data read from the DB.
* @param row array containing one read record.
*/
private function init($row){
$this->id = $row['ID'];
$this->usergroupId = $row['USERGROUP_ID'];
$this->user = $row['USER'];
$this->passwd = $row['PASSWD'];
}
/**
* Returns the string representation of this obbject
* @return String repesentation ofUser
*/
public function toString(){
$s = '';
$s .= 'ID: '.$this->id;
$s .= 'USERGROUP_ID: '.$this->usergroupId;
$s .= 'USER: '.$this->user;
$s .= 'PASSWD: '.$this->passwd;
return $s;
}
public function loadAll(){
$user = new User();
$rows = $user->dao->getData("");
$users = array();
for ($index = 0; $index < sizeof($rows); $index++) {
$d = new Document();
$d->init($rows[$index]);
array_push($users,$d);
}
return $users;
}
/**
*
* Load the User uniquely by its primary key.
* @param id primary key
* @return Instance of {@link User}
*/
static function loadUnique($id){
$user = new User();
$rows = $user->dao->getData("`ID`='$id'");
$user->init($rows[0]);
return $user;
}
/**
* Insert this object into the DB
* @return new id (auto increment value) genereated
*/
private function insert(){
$list = array("ID"=>$this->id, "USERGROUP_ID"=>$this->usergroupId, "USER"=>$this->user, "PASSWD"=>$this->passwd);
return $this->dao->insertRecord($list);
}
/**
* Update this object into the DB
* @return number of updated records
*/
private function update(){
$list = array("ID"=>$this->id, "USERGROUP_ID"=>$this->usergroupId, "USER"=>$this->user, "PASSWD"=>$this->passwd);
$where = "`ID`='$id'";
return $this->dao->updateRecord($list,$where);
}
/**
* Initialize the business object with data read from the DB.
*/
private function initPOST(){
if (isset($_POST['id'])){
$this->id = $_POST['id'];
}
if (isset($_POST['usergroupId'])){
$this->usergroupId = $_POST['usergroupId'];
}
if (isset($_POST['user'])){
$this->user = $_POST['user'];
}
if (isset($_POST['passwd'])){
$this->passwd = $_POST['passwd'];
}
}
/**
* Initialize the business object with data read from the DB.
*/
private function initGET(){
if (isset($_GET['id'])){
$this->id = $_GET['id'];
}
if (isset($_GET['usergroupId'])){
$this->usergroupId = $_GET['usergroupId'];
}
if (isset($_GET['user'])){
$this->user = $_GET['user'];
}
if (isset($_GET['passwd'])){
$this->passwd = $_GET['passwd'];
}
if (isset($_GET['email'])){
$this->email = $_GET['email'];
}
}
}
?>

Schlagworte: , , , , , ,

Kommentieren

Sie müssen angemeldet sein, um kommentieren zu können.