Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOtherTutorials

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old July 12th, 2003, 06:01 PM
jmc jmc is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 9 jmc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
A Framework for Persisting Data Relationships

Can anyone help me modify Jakes script from the article.

I have changed what I think is necessary to make it work, the form works and tells me to check the database but no records are added.

My database contains the following tables etc.

cd_artists
==========
Artist_ID
CompactDisc_Artist

cd_compactdiscs
===============
CompactDisc_ID
CompactDisc_Title

cd_artist2cd
============
Artist_ID
CompactDisc_ID

I think the problem is to do with the arrays.

php Code:
Original - php Code
  1.  
  2. <?php
  3. //cd.php
  4. if (isset($_POST['submit'])) {
  5. echo "submitting <br>";
  6. include_once("cd_classes.php");
  7.  
  8. // new Compactdisc:;
  9. $newCompactdisc = new Compactdisc;
  10. $newCompactdisc->title = ($_POST['title']);
  11. $newCompactdisc->PersistNew();
  12.  
  13. // new Artist
  14. $newArtist = new Artist;
  15. $newArtist ->artistname = ($_POST['artist']);
  16. $newArtist->PersistNew();
  17.  
  18. // new Link
  19. $newCompactdiscArtistLink = new CompactdiscArtistLink;
  20. $newCompactdiscArtistLink ->artist = $newArtist->id;
  21. $newCompactdiscArtistLink ->cd = $newCompactdisc->id;
  22. $newCompactdiscArtistLink->PersistNew();
  23.  
  24. echo "done, check out the database";
  25. } else {
  26. ?>
  27. <html>
  28. <form method="POST" action="cd.php">
  29. <table>
  30.   <tr>
  31.     <td>Compact Disc Title:</td>
  32.     <td><input name="title" /></td>
  33.   </tr>
  34.   <tr>
  35.     <td>Compact Disc Artist:</td>
  36.     <td><input name="artist" /></td>
  37.   </tr>
  38.   <tr>
  39.     <td><input type="submit" name="submit" value="submit" /></td>
  40.     <td><input type="reset" /></td>
  41.   </tr>
  42. </table>
  43. </form>
  44. </html>
  45. <?php
  46. }
  47. ?>
  48. =======
  49.  
  50. <?php
  51. // cd_classes.php
  52.  
  53. // A database persitable object, pretty useless unless you extend it.   
  54. class Persistable {
  55.  
  56.       // Unique database identifier.
  57.       var $id;
  58.       // Table name that is used in the database
  59.       var $table;
  60.       // Map of column labels in the database to object variables
  61.       var $columns = array();
  62.  
  63.       // Persist a new object in the database.
  64.       function PersistNew() {
  65.         mysql_connect('localhost', 'xxx', 'xxx')
  66.                   or die("Could not connect: " . mysql_error());
  67.         mysql_select_db('jamesmcl_cms')
  68.               or die ("Unable to select database");
  69.        
  70.         $sql = "INSERT INTO `".$this->table."` ";
  71.         $labels = "(`id` ";
  72.         $values = ") VALUES ('' ";
  73.  
  74.         foreach ($this->columns as $column) {
  75.           $labels .= ", `".$column[0]."` ";
  76.           $temp = '$this->'.$column[1];
  77.           $values .= ", '"."$temp"."' ";   
  78.           unset($temp);
  79.         }
  80.  
  81.         $sql .= $labels.$values.")";
  82.         eval ("$sql = "$sql";");
  83.         $result = mysql_query($sql);
  84.         $this->id = mysql_insert_id();
  85.       }
  86. }// End Class Persistable
  87.  
  88. class Book extends Persistable {
  89.  
  90.       var $title;//String of book title.
  91.  
  92.       function Book() { //Constructor for Book.
  93.         $this->table = 'test_books';
  94.         $this->columns = array( array('title','title')
  95.                               );
  96.       }
  97. } // End Class Book
  98.  
  99. class Author extends Persistable {
  100.    
  101.       var $fname; // String of author first name. 
  102.       var $lname; // String of author last name.
  103.    
  104.       function Author() { //Constructor for Author.
  105.         $this->table = 'test_authors';
  106.         $this->columns = array( array('firstname','fname'),
  107.                                 array('lastname','lname')
  108.                               );
  109.       }
  110. } // End Class Author
  111.  
  112. class BookAuthorLink extends Persistable {
  113.    
  114.       var $book; // integer book unique id. 
  115.       var $author; //  integer author unique id.
  116.    
  117.       function BookAuthorLink() { //Constructor for Author.
  118.         $this->table = 'test_bookauthorlink';
  119.         $this->columns = array( array('book','book'),
  120.                                 array('author','author')
  121.                               );
  122.       }
  123. } // End Class BookAuthorLink
  124.  
  125. class Compactdisc extends Persistable {
  126.  
  127.       var $title;//String of book title.
  128.  
  129.       function Compactdisc() { //Constructor for Compactdisc.
  130.         $this->table = 'cd_compactdiscs';
  131.         $this->columns = array( array('CompactDisc_Title','title')
  132.                               );
  133.       }
  134. } // End Class Compactdisc
  135.  
  136. class Artist extends Persistable {
  137.    
  138.       var $artistname; // String of artist name. 
  139.      
  140.    
  141.       function Artist() { //Constructor for Artist.
  142.         $this->table = 'cd_artists';
  143.         $this->columns = array( array('CompactDisc_Artist','artistname')
  144.                                );
  145.       }
  146. } // End Class Artist
  147.  
  148. class CompactdiscArtistLink extends Persistable {
  149.    
  150.       var $cd; // integer cd unique id. 
  151.       var $artist; //  integer artist unique id.
  152.    
  153.       function CompactdiscArtistLink() { //Constructor for CompactdiscArtist.
  154.         $this->table = 'cd_artist2cd';
  155.         $this->columns = array( array('cd','cd'),
  156.                                 array('artist','artist')
  157.                               );
  158.       }
  159. } // End Class CompactdiscArtistLink
  160. ?>

Reply With Quote
  #2  
Old July 14th, 2003, 06:18 AM
drevele drevele is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Medina, Ohio
Posts: 233 drevele User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via AIM to drevele Send a message via Yahoo to drevele
RE: A Framework for Persisting Data Relationships

Im not the smartest at arrays but array(array(
dosent seem right.
Why not just use: array('bla', 'bla');
Instead of having:
$my_array = array(array('Test1,'Test2'););
Array -
1. array-
1. Test1
2. Test2
Why not have
$my_array = array('Test1', 'Test2');
Array-
1. Test1
2. Test2

It is alot simpler

Reply With Quote
  #3  
Old July 14th, 2003, 06:49 PM
jmc jmc is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 9 jmc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: A Framework for Persisting Data Relationships

drevele,
the original code with the arrays works fine.

I have tried to change it to work with my cd tables and I cant see where I'm going wrong. I presume that its to do with the variables and database fields.

In the example

php Code:
Original - php Code
  1.  
  2. class Author extends Persistable {
  3.    
  4.       var $fname; // String of author first name. 
  5.       var $lname; // String of author last name.
  6.    
  7.       function Author() { //Constructor for Author.
  8.         $this->table = 'test_authors';
  9.         $this->columns = array( array('firstname','fname'),
  10.                                 array('lastname','lname')
  11.                               );
  12.       }
  13. } // End Class Author
  14.  


firstname and lastname are database fields

Any ideas

Reply With Quote
  #4  
Old July 14th, 2003, 11:40 PM
drevele drevele is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Medina, Ohio
Posts: 233 drevele User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Send a message via AIM to drevele Send a message via Yahoo to drevele
RE: A Framework for Persisting Data Relationships

php Code:
Original - php Code
  1.       function PersistNew() {
  2.         mysql_connect('localhost', 'xxx', 'xxx')
  3.                   or die("Could not connect: " . mysql_error());
  4.         mysql_select_db('jamesmcl_cms')
  5.               or die ("Unable to select database");
  6.        
  7.         $sql = "INSERT INTO `".$this->table."` ";
  8.         $labels = "(`id` ";
  9.         $values = ") VALUES ('' ";
  10.  
  11.         foreach ($this->columns as $column) {
  12.           $labels .= ", `".$column[0]."` ";
  13.           $temp = '$this->'.$column[1];
  14.           $values .= ", '"."$temp"."' ";   
  15.           unset($temp);
  16.         }

When is $this->table defined?

Reply With Quote
  #5  
Old July 15th, 2003, 06:30 AM
jmc jmc is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 9 jmc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: A Framework for Persisting Data Relationships

The tables are defined in each of the classes / functions. $this-> table etc.

php Code:
Original - php Code
  1.  
  2. class Author extends Persistable {
  3.    
  4.       var $fname; // String of author first name. 
  5.       var $lname; // String of author last name.
  6.    
  7.       function Author() { //Constructor for Author.
  8.         $this->table = 'test_authors';
  9.         $this->columns = array( array('firstname','fname'),
  10.                                 array('lastname','lname')
  11.                               );
  12.       }
  13. } // End Class Author
  14.  

Reply With Quote
  #6  
Old July 22nd, 2003, 08:24 PM
superalloy superalloy is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Washington DC
Posts: 58 superalloy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
RE: A Framework for Persisting Data Relationships

I think that the problem is coming from the database structure not working with the sql command that is generated, the tutorial says that
Quote:
Requirements
Basically I am dictating that all the tables that I am using in the database have an auto-incrementing primary key with the column name “id”. That way, after I INSERT the data to the database, I can retrieve the id and set the object id to that value.

having your id columns with names Artist_ID, ..., the sql does not execute properly (can't fill out the id field)

Reply With Quote
  #7  
Old July 26th, 2003, 03:04 PM
jmc jmc is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 9 jmc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: A Framework for Persisting Data Relationships

Thanks for the advice superalloy.
I changed the database fields as suggested.
However, I still can't update the database - somethings still wrong.

Can you help.

My database tables and fields are

cd_artist2cd
============
id
artist
cd

cd_artist
=========
id
CompactDisc_Artist

cd_compactdisc
==============
id
CompactDisc_Title

The code
php Code:
Original - php Code
  1.  
  2. <?php
  3. //cd.php
  4. if (isset($_POST['submit'])) {
  5. echo "submitting <br>";
  6. include_once("cd_classes.php");
  7.  
  8. // new Compactdisc:;
  9. $newCompactdisc = new Compactdisc;
  10. $newCompactdisc->title = ($_POST['title']);
  11. $newCompactdisc->PersistNew();
  12.  
  13. // new Artist
  14. $newArtist = new Artist;
  15. $newArtist ->artistname = ($_POST['artist']);
  16. $newArtist->PersistNew();
  17.  
  18. // new Link
  19. $newCompactdiscArtistLink = new CompactdiscArtistLink;
  20. $newCompactdiscArtistLink ->artist = $newArtist->id;
  21. $newCompactdiscArtistLink ->cd = $newCompactdisc->id;
  22. $newCompactdiscArtistLink->PersistNew();
  23.  
  24. echo "done, check out the database";
  25. } else {
  26. ?>
  27. <html>
  28. <form method="POST" action="cd_classes.php">
  29. <table>
  30.   <tr>
  31.     <td>Compact Disc Title:</td>
  32.     <td><input name="title" /></td>
  33.   </tr>
  34.   <tr>
  35.     <td>Compact Disc Artist:</td>
  36.     <td><input name="artist" /></td>
  37.   </tr>
  38.   <tr>
  39.     <td><input type="submit" name="submit" value="submit" /></td>
  40.     <td><input type="reset" /></td>
  41.   </tr>
  42. </table>
  43. </form>
  44. </html>
  45. <?php
  46. }
  47. ?>
  48. =======
  49. <?php
  50. // cd_classes.php
  51.  
  52. // A database persitable object, pretty useless unless you extend it.   
  53. class Persistable {
  54.  
  55.       // Unique database identifier.
  56.       var $id;
  57.       // Table name that is used in the database
  58.       var $table;
  59.       // Map of column labels in the database to object variables
  60.       var $columns = array();
  61.  
  62.       // Persist a new object in the database.
  63.       function PersistNew() {
  64.         mysql_connect('localhost', 'xxx', 'xxx')
  65.                   or die("Could not connect: " . mysql_error());
  66.         mysql_select_db('jamesmcl_cms')
  67.               or die ("Unable to select database");
  68.        
  69.         $sql = "INSERT INTO `".$this->table."` ";
  70.         $labels = "(`id` ";
  71.         $values = ") VALUES ('' ";
  72.  
  73.         foreach ($this->columns as $column) {
  74.           $labels .= ", `".$column[0]."` ";
  75.           $temp = '$this->'.$column[1];
  76.           $values .= ", '"."$temp"."' ";   
  77.           unset($temp);
  78.         }
  79.  
  80.         $sql .= $labels.$values.")";
  81.         eval ("$sql = "$sql";");
  82.         $result = mysql_query($sql);
  83.         $this->id = mysql_insert_id();
  84.       }
  85. }// End Class Persistable
  86.  
  87. class Book extends Persistable {
  88.  
  89.       var $title;//String of book title.
  90.  
  91.       function Book() { //Constructor for Book.
  92.         $this->table = 'test_books';
  93.         $this->columns = array( array('title','title')
  94.                               );
  95.       }
  96. } // End Class Book
  97.  
  98. class Author extends Persistable {
  99.    
  100.       var $fname; // String of author first name. 
  101.       var $lname; // String of author last name.
  102.    
  103.       function Author() { //Constructor for Author.
  104.         $this->table = 'test_authors';
  105.         $this->columns = array( array('firstname','fname'),
  106.                                 array('lastname','lname')
  107.                               );
  108.       }
  109. } // End Class Author
  110.  
  111. class BookAuthorLink extends Persistable {
  112.    
  113.       var $book; // integer book unique id. 
  114.       var $author; //  integer author unique id.
  115.    
  116.       function BookAuthorLink() { //Constructor for Author.
  117.         $this->table = 'test_bookauthorlink';
  118.         $this->columns = array( array('book','book'),
  119.                                 array('author','author')
  120.                               );
  121.       }
  122. } // End Class BookAuthorLink
  123.  
  124. class Compactdisc extends Persistable {
  125.  
  126.       var $title;//String of book title.
  127.  
  128.       function Compactdisc() { //Constructor for Compactdisc.
  129.         $this->table = 'cd_compactdiscs';
  130.         $this->columns = array( array('CompactDisc_Title','title')
  131.                               );
  132.       }
  133. } // End Class Compactdisc
  134.  
  135. class Artist extends Persistable {
  136.    
  137.       var $artistname; // String of artist name. 
  138.      
  139.    
  140.       function Artist() { //Constructor for Artist.
  141.         $this->table = 'cd_artists';
  142.         $this->columns = array( array('CompactDisc_Artist','artistname')
  143.                                );
  144.       }
  145. } // End Class Artist
  146.  
  147. class CompactdiscArtistLink extends Persistable {
  148.    
  149.       var $cd; // integer cd unique id. 
  150.       var $artist; //  integer artist unique id.
  151.    
  152.       function CompactdiscArtistLink() { //Constructor for CompactdiscArtist.
  153.         $this->table = 'cd_artist2cd';
  154.         $this->columns = array( array('cd','cd'),
  155.                                 array('artist','artist')
  156.                               );
  157.       }
  158. } // End Class CompactdiscArtistLink
  159. ?>
  160.  



Reply With Quote
  #8  
Old August 1st, 2003, 01:37 AM
superalloy superalloy is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Washington DC
Posts: 58 superalloy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
RE: A Framework for Persisting Data Relationships

Are the id fields autonumber?


Sorry I didn't mean to resolve the thread.

Reply With Quote
  #9  
Old August 1st, 2003, 06:45 AM
jmc jmc is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 9 jmc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: A Framework for Persisting Data Relationships

Yes, the id fields are autonumber.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOtherTutorials > A Framework for Persisting Data Relationships


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek