Archive for April, 2010

Usage of Data Transfer Object (DTO) Pattern in PHP

April 27th, 2010

Data Transfer Object is a heavily used design pattern used in the development of web applications requiring database interaction in J2EE. It makes the life of a developer highly easy in managing the data that is being transferred and provides a secured way of transferring the data between layers.

When i switch from J2EE to PHP and with the release of PHP5, it suddenly comes to my mind to use this concept along with Data Access Object pattern (which i will explain in my next post) from J2EE in PHP  and it worked wonderfully for me to meet any level of complex applications.

What is a Data object?

First of all, we need to understand as what is a data object. A data object is a class representing a table in the database with the field names exactly same as database column names. It contains the getter and setter methods for these fields.

Let’s say i have a table called tbl_employee having columns as

- id (int 10)

- name ( varchar(50))

Now, the dataobject would typically look like

public class Employee()

{

var $id;

var $name;

public function getId()

{

return $this->id;

}

public function setId($tempId)

{

$this->id=$tempId;

return $this->id;

}

public function getName()
{
return $this->name;
}
public function setName($tempName)
{
$this->name=$tempName;
return $this->name;
}

Now the next point is about using it and see how helpful it would turn out to be. The following function interacts with the database thus assuming database connection is available.

public function getEmployeeList()

{

$employeeList = array();

$query = “select * from tbl_employee order by name”;

$result = mysql_query($query);

while ($row=mysql_fetch_array($result))

{

$tempEmployeeDO = new EmployeeDO();

$tempEmployeeDO->setId($row['id']);

$tempEmployeeDO->setName($row['name']);

$employeeList[count($employeeList)] = $tempEmployeeDO;

}

return $employeeList;

}

Thus the above function returns the list of employees containing data objects. On the front end you need to iterate through the list where each element is a DO and than use getter method to retrieve the values.

Start using it and you will realize that it makes your life pretty easy during development.

Enjoy!!

Ushainformatique Development Team

Database modeling with Microsoft Visio for an Existing MySQL Database

April 18th, 2010

In a SDLC, creating a database model diagram is an important step that needs to be taken to achieve the following

- Create a clear visualization of the Database for the system which makes the database understanding pretty easier

- It helps in derive out the classes required to be created by just looking into the database modeling diagram in depth

If you are not able to buy the paid tool for UML modeling such as rational rose etc., it’s still achievable through Microsoft Visio. Following are the steps that need to be performed to create a model diagram from an existing MySQL database in Microsoft Visio which is called as Reverse Engineering

- First of all, download the ODBC driver for MySQL from the location http://dev.mysql.com/downloads/connector/odbc/5.1.html. Download the installable by selecting the appropriate one based on  your machine configuration (32 bit or 64 bit)

- Install the mysql connector

- After installation, open visio

- Go to File->New->Database->Database Model

- From Database Menu, select Reverse Engineer

- On the Create New Datasource screen displayed, click on User Data Source

- Provide a datasource name

- Select the mysql connector driver, click on Next

- Click Finish and your new datasource would be added and would come as selected

- Click on Next

- Enter the valid username and password to create the connection using the datasource

- Select Object Type to reverse engineers

- You would be asked to select the database

- On selecting database, tables would be displayed

- Select the tables you want to create the datamodel for

- On selecting the tables, you would be asked to create the model with shape or without shape

- I select the shaped one and the model is generated

It’s really a helpful tool for the beginner to start creating the Database Modeling Diagram from an existing Database

Code Formatting in Netbeans 6.8

April 17th, 2010

In any project in web world, code formatting plays a key role during the development of the project as it makes the code review process very simple at least from readability point of view.

The more readable your code is the more easy it is to review it. Now, how to format the code in Netbeans 6.8.

There are two levels at which formatting can be performed in Netbeans 6.8.

1) At a global level – It’s applicable across all the projects for which netbeans would be used

2) At a project level – It is specific to a particular project

For the first option

a) Go to Tools->Options->Editor-> Formatting

b) Please select the language for which you want to apply the formatting, for me it is PHP

c) Select the category, which could be Tabs and Indent or Braces

d) Put your option for Tabs and Indent in terms of tabs or if you choose to go with spaces

e) For braces, you can select new line, same line or preserve existing

This is pretty useful as if you are aware of it, it saves lot of effort from your end.

Enjoy working with Netbeans 6.8 !!