Home arrow PHP arrow Page 5 - Building Object-Oriented Database Interfaces in PHP: Working with Multiple Data Access Objects

Adding rows to both tables: working with multiple data access objects - PHP

In previous articles in this series, we examined the concept of database interfaces, and saw source code for a "DBIGenerator" class. Working with multiple DB interfaces permits the accomplishment of several operations on many tables at the same time. Alejandro Gervasio demonstrates the power of this concept, with examples.

  1. Building Object-Oriented Database Interfaces in PHP: Working with Multiple Data Access Objects
  2. Getting in touch with a DB Interface: A brief look at the "DBI Generator" class
  3. Code generator in a nutshell: one class that generates another one
  4. Some useful examples: a practical usage of the "DBIGenerator" class
  5. Adding rows to both tables: working with multiple data access objects
By: Alejandro Gervasio
Rating: starstarstarstarstar / 15
August 24, 2005

print this article



Having created a couple of DB Interface classes that fit into the application as single communication points to each table, it's fairly easy to insert new data into the tables. Considering this particular case, we're utilizing a regular POST form to accomplish this task. The multi-row insertion process is performed in the following sequence: first, we insert POST data into the "users" table using the $users object returned by the "DBIGenerator" class, as shown in the lines below:

//insert new user into "users" table
echo 'User '.$users->getid().' added successfully<br />';

Of course, before directly accepting user data coming from a POST/GET/COOKIE request, a validation operation should always be performed, to quickly reject invalid data. However, for keeping the sample code uncluttered, that will be left as a possible improvement. Anyway, due to the fact that each database interface is a centralized data access mechanism, data verification should be easy to implement.

The above code inserts a new row into the "users" table, adding "firstname", "lastname", and "email" values, simply using the $users object's API (Application Programming Interface). Finally the data is inserted via the "submit()" method.

But, we need to maintain the relationship between the "users" and "messages" tables. So, each time a new user is added to the table, the "messages" table needs to be updated. Therefore, the user-submitted message is added to this table, using the methods provided by the "$messages" object:

// insert new message into "messages" table
echo 'Message '.$messages->getid().' added successfully';

By doing so, we're keeping track of the messages submitted by a user, since the "user_id" field is populated with the "ID" property of the "$users" object. The following line illustrates this process:


That's about it. The multi-row insertion operation has been accomplished through the data access objects tied to each particular database table. Definitely, the above approximation is very powerful when applied to large web projects, where a set of data access objects carry out centralized DML operations.


In this series of articles, we've taken a detailed look at the concept and application of database interfaces, exploring the numerous scenarios where they can be implemented. Also, during the development process, we've used the "Factory" Design Pattern, to dynamically create data access objects. Hopefully, the whole experience has been instructive and rich.

However, there are more things to be reviewed. Since PHP is a very fast-growing language, the last part of this series implements database interfaces in PHP 5, taking advantage of the powerful Object Model that this newly developed version offers to programmers. See you in the last part!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: