DataObject is a design pattern that incapsualtes the database with a simple to use interface. This alows the Controller to interact with the database layer in an SQL’less way. To acomplish this you build a base DataObject abstract class that implements the base functionality and sets up the interface for a Data Object. Then for each of your ‘Business Objects’ (such as customer), you extend the DataObject class and implement the various methods provided by DataObject. (see attached code for details).
You’ll in the posted PHP files below that the code gets more ‘complex’ as we move down the stack (Controller->BusinessObject->DataObject). This is the beauty of OOP since we have implemented the DataObject only once in our application (it may be used by many Business Objects). As long as we maintain its interface, we are free to refactore its implementation details and add functionallity. And there is only one file to change.
Retrival of object from the database
In the simple case of the controller needing to retrieve a customer. Traditionally this might involve building a query of something like…
This process involes the Controller having far too much knowelge of the Database layer. This tight coupling should be avoided.
The same process using a database object would go something like this…
Search the database for an Object
This is quite elegant with DO’s. Just simply create a new DO, set what you do know about what you are seaching for then execute the object find method.
Saving and Updating Objects
Again this is very simple also. For a populated customer DO object, just issue its insert method and to update, use the update method.
Here is a Proof of Concept Controller PHP script
Here is the Customer Data Object that extends DataObject
