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();