php

Today, i am going to teach you, how to develop object relational mapping database application in PHP using object oriented programming methodology. I love to code in object oriented method to reduce redundancy, encapsulation and many more. Let’s begin….

 

1. First create the database called ‘ORM‘ using phpmyadmin and create the ‘person‘ table

 

 

2. We create four files:

 

 

3. Data Access Layer, database.php

 

Above class code, basically contains connection parameters, resultset and SELECT statement generating variables which are all private.

Likewise, it contains one static variable and method to get instance to this class to avoid everytime connect the database server. The main functions are connect() for connecting database, executeQuery() for executing the query statement, loadSingleObject() and loadObjectList() to return the resultset as associative array.

Similarly, we have methods to generate select statement for any table, any column, any filter, ordering and limitations using Method Chaining functionality of PHP5.

If it were in MVC pattern, it comes under model section.

 

4. Object Relational Mapping Layer, entity.php

 

This class contains protected variables and one protected method.  Protected variable is accessible in inheriting classes. They are tablename, primary keys and autoincrement columns. We build query using prepareQuery() for create, retrieval, update and delete of record from table. Here, we use build query optimally for more than one primary keys and more than one autoincrement columns. It also contains bind() method to set the variables of object class extending entity class passing as an array. Finally, it contains the method to add(), update(), remote() and load(), loadMultiple() to operate on instance of class extending entity class.

If it were implemented in MVC pattern, then it also comes under model section.

 

5. Object class, Person.php

 

The above code pretty straight forward, the class variables are the column names of person table and $tablename, $pkeys and $aikeys value are set inherited from Entity class which is extended by Person class. It also comes under model section in MVC pattern.

 

5. Business Logic Layer, index.php

 

 

Here, we create an instance of database class object, in singleton pattern. So, only one instance of the database object throughout the application so we don’t have to connect database everytime. Then, we create instance of Person class. After that, every operation is performed on that $person object. That’s how its an object oriented programming style and now we don’t have to write the SQL insert, delete, update and select code. We have abstracted all those functionalities in Entity class, where table’s column are mapped to object’s variables and object can perform several operation on itself. If it were implemented in MVC pattern, it comes under controller section.

That’s it. Cheers!! Thank you reading!! If you replace the mysqli above with PDO with everything same, then i would be much happy and please send me the link. Have a good programming ahead…

 

In next tutorial, i will show you how to generate entities class automatically from database  that fits in our above application….Please stay tuned..

 

Download Source Code