HomePHP 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.
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 $users->setfirstname($_POST['firstname']); $users->setlastname($_POST['lastname']); $users->setemail($_POST['email']); $users->submit(); 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 $messages->setmessage($_POST['message']); $messages->setuser_id($users->getid()); $messages->submit(); 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!