473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Object Oriented design using a database.

I wanted to know if is a good idea to create classes that mirror
exactly the structure of a database. This might sound a bit fuzzy but
I'l explain myself with an example:

[sql]
CREATE TABLE `pl_cities` (
`city_id` int(10) NOT NULL auto_increment,
`city_name` varchar(16) NOT NULL default '',
`city_coordinates` varchar(10) NOT NULL default '',
`city_country` enum('germany','switzerlan','austria') NOT NULL
default 'germany',
PRIMARY KEY (`city_id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
[/sql]

This is the stuff I have in my database.
Now I make the following class (not really, I made a super-class which
handles the database interaction, but we'll assume my class is this
one):

Expand|Select|Wrap|Line Numbers
  1. class City{
  2. var $vars = array("id" => 0);
  3. var $tableprefix = "city_";
  4. var $tablename = "pl_cities";
  5. var $errorMsg = "No Error";
  6.  
  7.  
  8. /****************************************************************************
  9. * Constructor loads the default data into the variables.
  10. */
  11. function PlanerClass(){
  12. foreach($this->vars as $key => $value){
  13. $this->$key = $this->vars[$key];
  14. }
  15. }
  16.  
  17.  
  18. /****************************************************************************
  19. * Unserialize the object from
  20. */
  21. function unserial($inst){
  22. //$inst = unserialize($inst);
  23. $data = get_object_vars($inst);
  24. foreach($this->vars as $key => $value){
  25. //print($key." => ".$value." => ".$data[$key]."<br>\n");
  26. if(empty($data[$key])){
  27. $this->$key = $value;
  28. }else{
  29. $this->$key = $data[$key];
  30. }
  31. }
  32. return count($this->vars);
  33. }
  34.  
  35. function doprint(){
  36. print("<p><b>".$this->tablename."</b><br>\n");
  37. foreach($this->vars as $key => $value){
  38. print($key." => ".$this->$key."<br>\n");
  39. }
  40. print("</p>\n\n");
  41. }
  42.  
  43. function load($o_id){
  44. global $mysql;
  45. $sql = "SELECT * FROM ".$this->tablename." WHERE ";
  46. $sql .=$this->tableprefix."id='".$o_id."' ";
  47. if(array_key_exists("name",$this->vars))
  48. $sql .= "OR ".$this->tableprefix."name='".$o_id."' ";
  49. $sql .="LIMIT 1";
  50. $mysql->RawQuery($sql);
  51. $data = $mysql->FetchArray();
  52. $this->inject($data);
  53. }
  54.  
  55. function inject($data){
  56. foreach($this->vars as $key => $value){
  57. $this->$key = $data[$this->tableprefix.$key];
  58. }
  59. }
  60. function save(){
  61. global $mysql;
  62. $sql = "UPDATE ".$this->tablename." SET ";
  63. $splits = array();
  64. foreach($this->vars as $key => $value)
  65. $splits[] =
  66. $this->tableprefix.$key."='".addslashes($this->$key)."'";
  67. $sql .= implode(", ",$splits);
  68. $sql .= " WHERE ".$this->tableprefix."id='".$this->id."' LIMIT 1;";
  69. return $mysql->RawQuery($sql);
  70. }
  71. }
  72.  
At the top I have an array of variable-names which are loaded from the
database if an ID has been specified. As you might already have guessed
I generate alot of SQL-Queries by using these classes (even if I do use
City::inject() which is used to load a recordset). Is it a good idea?
Snyke

Jul 17 '05 #1
2 1891
I don't see why not, for the simple reason that this is exactly what I do.
Take a look at http://www.tonymarston.co.uk/php-mys...seobjects.html.
I have actually built a full-blown infrastructure using this technique which
is described at http://www.tonymarston.co.uk/php-mys...structure.html
with a small sample application described at
http://www.tonymarston.co.uk/php-mys...tion.htmlwhich can be
run online or you can download all the code.

I must warn you that there are some OO aficionados who think that creating a
separate class for each database table is OOO (out of order). Their
criticisms and my replies are documented at
http://www.tonymarston.co.uk/php-mys...d-bad-oop.html

HATH.

--
Tony Marston

http://www.tonymarston.net

"Christian Decker" <De**************@gmail.com> wrote in message
news:ci********@odak26.prod.google.com...
I wanted to know if is a good idea to create classes that mirror
exactly the structure of a database. This might sound a bit fuzzy but
I'l explain myself with an example:

[sql]
CREATE TABLE `pl_cities` (
`city_id` int(10) NOT NULL auto_increment,
`city_name` varchar(16) NOT NULL default '',
`city_coordinates` varchar(10) NOT NULL default '',
`city_country` enum('germany','switzerlan','austria') NOT NULL
default 'germany',
PRIMARY KEY (`city_id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
[/sql]

This is the stuff I have in my database.
Now I make the following class (not really, I made a super-class which
handles the database interaction, but we'll assume my class is this
one):

Expand|Select|Wrap|Line Numbers
  1.  class City{
  2.  var $vars = array("id" => 0);
  3.  var $tableprefix = "city_";
  4.  var $tablename = "pl_cities";
  5.  var $errorMsg = "No Error";
  6.  /****************************************************************************
  7.  * Constructor loads the default data into the variables.
  8.  */
  9.  function PlanerClass(){
  10.  foreach($this->vars as $key => $value){
  11.  $this->$key = $this->vars[$key];
  12.  }
  13.  }
  14.  /****************************************************************************
  15.  * Unserialize the object from
  16.  */
  17.  function unserial($inst){
  18.  //$inst = unserialize($inst);
  19.  $data = get_object_vars($inst);
  20.  foreach($this->vars as $key => $value){
  21.  //print($key." => ".$value." => ".$data[$key]."<br>\n");
  22.  if(empty($data[$key])){
  23.  $this->$key = $value;
  24.  }else{
  25.  $this->$key = $data[$key];
  26.  }
  27.  }
  28.  return count($this->vars);
  29.  }
  30.  function doprint(){
  31.  print("<p><b>".$this->tablename."</b><br>\n");
  32.  foreach($this->vars as $key => $value){
  33.  print($key." => ".$this->$key."<br>\n");
  34.  }
  35.  print("</p>\n\n");
  36.  }
  37.  function load($o_id){
  38.  global $mysql;
  39.  $sql = "SELECT * FROM ".$this->tablename." WHERE ";
  40.  $sql .=$this->tableprefix."id='".$o_id."' ";
  41.  if(array_key_exists("name",$this->vars))
  42.  $sql .= "OR ".$this->tableprefix."name='".$o_id."' ";
  43.  $sql .="LIMIT 1";
  44.  $mysql->RawQuery($sql);
  45.  $data = $mysql->FetchArray();
  46.  $this->inject($data);
  47.  }
  48.  function inject($data){
  49.  foreach($this->vars as $key => $value){
  50.  $this->$key = $data[$this->tableprefix.$key];
  51.  }
  52.  }
  53.  function save(){
  54.  global $mysql;
  55.  $sql = "UPDATE ".$this->tablename." SET ";
  56.  $splits = array();
  57.  foreach($this->vars as $key => $value)
  58.  $splits[] =
  59.  $this->tableprefix.$key."='".addslashes($this->$key)."'";
  60.  $sql .= implode(", ",$splits);
  61.  $sql .= " WHERE ".$this->tableprefix."id='".$this->id."' LIMIT 1;";
  62.  return $mysql->RawQuery($sql);
  63.  }
  64.  }
  65.  

At the top I have an array of variable-names which are loaded from the
database if an ID has been specified. As you might already have guessed
I generate alot of SQL-Queries by using these classes (even if I do use
City::inject() which is used to load a recordset). Is it a good idea?
Snyke

Jul 17 '05 #2
"Christian Decker" <De**************@gmail.com> wrote in message
news:ci********@odak26.prod.google.com...
I wanted to know if is a good idea to create classes that mirror
exactly the structure of a database. This might sound a bit fuzzy but
I'l explain myself with an example:


I don't know about mirroring "exactly." I do something very similiar, but
the classes are structured with ease of programming in mind. Records from
multiple tables are placed into one object when they belong together
conceptually. For example, I use a UserProfile class to hold all the info
about a user.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ggg | last post by:
I'm looking for a complete project/application done with heavy use of of object-oriented programming & design. Preferably something well documented and/or commented so that I can pick it apart...
34
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. ...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
8
by: Dale | last post by:
I've searched Amazon and read probably 100 reviews but can't find what seems to be any book that is widely accepted as the definitive book on object oriented programming design and techniques. And...
4
by: scottrm | last post by:
I am fairly new to oo design and I am looking at developing an object oriented asp.net application which will be built on top of a relational database. I have read quite a bit of the theory but...
2
by: Chris Asaipillai | last post by:
Hi there Im starting a new course this Saturday. OBJECT ORIENTED DEVELOPMENT WITH VISUAL BASIC .NET and will cover the following concepts and principles. a.. Be able to build VB.NET...
13
by: TS | last post by:
Say i have a class car with properties: Color, Make, Model, Year, DriverID And a Driver class with properties: DriverID, Name The driverID PRIVATE property is the id of the driver from say a...
4
by: Andrew Taylor | last post by:
Hi, I've been using PHP for a long time, I have designed and developed a number of mid-range systems. I've always used a procedural approach. I fully understand the concept of OO, I know all the...
46
by: ajba74 | last post by:
Hi fellows, I am reading some books to learn the C programming language, and sometimes I have the feeling that when somebody becomes a C expert, he must learn a more modern and object-oriented...
5
by: virtualadepts | last post by:
I have code here that explains my object oriented design model. I've been reading about other design models from what is documented on wikipedia about the key book on the subject:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.