Database Articles
  Home arrow Database Articles arrow A Framework for Persisting Data Relationships
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE ARTICLES

A Framework for Persisting Data Relationships
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2003-01-21

    Table of Contents:
  • A Framework for Persisting Data Relationships
  • The Classes
  • Trying it Out
  • What's next?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A Framework for Persisting Data Relationships


    (Page 1 of 4 )

    This tutorial will show you how to create relationships in your databases. PHP classes are provided that will perform the job.

    By : Jake Huffman

    I have been working on the basic design for a php mysql web application lately and in the process of trying to "do it right" I have been taking a look at lots of code and lots of tutorials. What I am going to show in this tutorial is the way that I managed to solve one of the problems that I was having. Specifically, I had a good database model with several tables with relationships, including standard link tables with nothing more than the primary key from different tables in them, but I was trying to populate all of the tables with the same php script. The example code that is provided is a simple example where a form is used to enter a book title and a book author. When processed, the author information is entered into one database table, the book information is entered into another database table, and the primary keys from the book and the author are entered into a link table. I couldn't find any tutorials that dealt with this situation, so this is what I came up with. . Having said that, keep in mind that there are a few tweaks that could be made to improve the efficiency of how this works, and I haven't spent the time to really work through it well. So if you give me your improvements, I would greatly appreciate them.

    This method comes, to a large degree, out of the basic concept laid down in the January 2003 issue of php|a in the article titled "Implementing Database Persistence Layers in PHP" by Shawn Bedard. The basic concept involves a little object-oriented programming and a direct correlation between your database and your objects. The first step for me was to establish a basic object that can be extended into anything that you want to persist in the database. In the code it is the class named Persistable. For the most part, it can't really be used by itself, but by doing a good job on the methods (functions) in the class; it can be easily extended to create anything that you want to store in the database. The real heart of what I'm dealing with is in the method PersistNew(). Basically, all it does it take the current values of the object, and create an SQL INSERT statement using those values. When the INSERT is complete, it retrieves the id from the database and sets the id for the object to the database id.

    The real advantage of this method is that using one form to populate several tables in the database with linking tables only takes a few lines of code (assuming that the Persistable class was extended to create everything that you want to store in the database). As an example: Let's say that I want to use a single form to store information about a book, the author and link the book to the author. Don't worry about why, just say I need a many to many relationship between books and authors. But since I want someone to like the software, I don't want them to create each book, then the author, then link them all, I want it all done at once. So how does it all look?

    <?php
    $newBook 
    = new Book;
    $newBook-&gt;title $_POST['title'];
    $newBook-&gt;PersistNew();

    $newAuthor = new Author;
    $newAuthor-&gt;firstname $_POST['firstname'];
    $newAuthor-&gt;lastname $_POST['lastname'];
    $newAuthor-&gt;PersistNew();

    $newBookAuthorLink = new BookAuthorLink;
    $newBookAuthorLink-&gt;author $newAuthor-&gt;id;
    $newBookAuthorLink-&gt;book $newBook-&gt;id;
    $newBookAuthorLink-&gt;PersistNew();
    ?>

    And that is it. Now of course you would probably like to know what those classes look like, right? Keep in mind that the really important one is the Persistable class so lets take a look at the code.

    More Database Articles Articles
    More By Codewalkers


       · 
       · Great teacher!I am just starting Apache/PHP/MySQL and haven't written a single...
       · I worked on a library like that for about 15 months, developing and improving it for...
     

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT