PHP Articles
Basic & Beginners
Advanced
Database
XML, Webservices
Design Patterns
Ajax
All Articles
Main Menu
Home
About PHP Hacks
Links
Contact Us
Search


Using PEAR DB DataObject PDF Print E-mail
Monday, 24 July 2006
DB_DataobjectDB_Dataobject is a package from PEAR that takes care of entity management. DataObject performs 2 tasks:

   1. Builds SQL statements based on the objects vars and the builder methods.

   2. Acts as a datastore for a table row.

    The core class is designed to be extended for each of your tables so that you put the data logic inside the data classes.
    
At one time or another your web application is going to require you to manage your entities in some way, usually modifying the object state by performing one of the following basic operations:

•    add (INSERT)
•    modify (UPDATE)
•    remove (DELETE)

The term in parentheses is the SQL analogy.  DataObjects (DO) encapsulates the way you interact with your data, making it a lot easier to manage object state.  The package has the following workflow:

•    Install the package in your PEAR hierarchy
•    Using the entities you decided on earlier, you create the appropriate tables in the db
•    Execute the DO createTables.php script which creates the entity classes in PHP for you which are analogous to the tables in your db
•    Use the DO API to manage all your objects (entities) in a uniform way, regardless of how many attributes you add/modify/delete

$myObj = new DataObjects_User  //  instantiate a User object
$myObj->get(12);  //  get a specific User object (row in the db)
$myObj->username = 'your username here';  //  getting/setting an attribute
$myObj->update();  //  updating the object state in the db
$myObj->insert();  //  insert if it's a new object
$myObj->delete();  //  delete the object

DB_Dataobject lets you manage complex entities in your web applications by encapsulating the object data and allowing you to modify object state more easily. Actually DO does a lot more, but now you have the basic concept.

Comments
Add NewSearchRSS
doemvsd - sdgsdg IP:66.45.220.195 | 2007-01-16 08:43:16
sdfsdfsdf
Only registered users can write comments!
 
Next >

Syndicate


Login Form





Lost Password?
No account yet? Register