Database Articles

  Home arrow Database Articles arrow Page 3 - A Framework for Persisting Data Relati...
DATABASE ARTICLES

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

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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    A Framework for Persisting Data Relationships - Trying it Out


    (Page 3 of 4 )

    Requirements

    In order for this technique to work, you must work with a mysql database (most other databases do not have the autoincrementing primary key). The tables that you are using must have an autoincrement primary key with the column name "id", unless you change the Persistable class around a little. And you need a webserver with php support, and a mysql database.

    Implementing

    First create the database stuff:

    CREATE TABLE test_books (
      id int(4) unsigned NOT NULL auto_increment,
      title varchar(15) NOT NULL default '',
      PRIMARY KEY  (id)
    ) TYPE=MyISAM COMMENT='book title list';

    CREATE TABLE test_authors (
      id int(4) unsigned NOT NULL auto_increment,
      firstname varchar(15) NOT NULL default '',
      lastname varchar(15) NOT NULL default '',
      PRIMARY KEY  (id)
    ) TYPE=MyISAM COMMENT='author list';

    CREATE TABLE test_bookauthorlink (
      id int(4) unsigned NOT NULL auto_increment,
      book int(4) NOT NULL default '',
      author int(4) NOT NULL default '',
      PRIMARY KEY  (id)
    ) TYPE=MyISAM COMMENT='book author link table';

    Then edit the database connection stuff in db_classes.php in the PersistNew() function (username, password, databasename). Located in the test_classes.php file.

    Load the test_classes.php (from above) and test.php (below) files.

    <?php
    //test.php
    if (isset($_POST['submit'])) {
    echo "submitting <br>";
    include_once("test_classes.php");

    // new Book:; 
    $newBook = new Book;
    $newBook->title = ($_POST['title']);
    $newBook->PersistNew();

    // new Author
    $newAuthor = new Author;
    $newAuthor ->fname = ($_POST['firstname']);
    $newAuthor ->lname = ($_POST['lastname']);
    $newAuthor->PersistNew();

    // new Link
    $newBookAuthorLink = new BookAuthorLink;
    $newBookAuthorLink ->author = $newAuthor->id;
    $newBookAuthorLink ->book = $newBook->id;
    $newBookAuthorLink->PersistNew();

    echo "done, check out the database";
    } else {
    ?>
    <html>
    <form method="POST" action="test.php">
    <table>
      <tr>
        <td>Book Title:</td>
        <td><input name="title" /></td>
      </tr>
      <tr>
        <td>Author First Name:</td>
        <td><input name="firstname" /></td>
      </tr>
      <tr>
        <td>Author Last Name:</td>
        <td><input name="lastname" /></td>
      </tr>
      <tr>
        <td><input type="submit" name="submit" value="submit" /></td>
        <td><input type="reset" /></td>
      </tr>
    </table>
    </form>
    </html>
    <?php
    }
    ?>

    Run the test.php file, enter some data in the form, and check out the database, see if it worked.

    More Database Articles Articles
    More By Codewalkers

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap