3

I've been working in Java lately, and I have OOP down. More recently I've been working on web based programming with jQuery, javascript, PHP, and most recently MySQL. With OOP its very easy to manage objects and read/write information about them. Now that I'm learning MySQL, I'm finding it hard to convert a simple OOP program to an online "program" that stores its data in a database. I've been playing around in Access, just so I can get a visual of the whole database, and can't seem to come to a good way of storing the data. For the purpose of learning this... here is a simple OOP class structure (Java) that I would like converted to a database (so to speak).

public class Tournament {

  ////////////////////////////////////////////
  //Variable Declaration
  ////////////////////////////////////////////
  Team[] theTeams;  //Array of Team Objects
  int numTeams;     //Number of Teams

  ////////////////////////////////////////////
  //Functions and stuff
  ////////////////////////////////////////////

}



public class Team {

  ////////////////////////////////////////////
  //Variable Declaration
  ////////////////////////////////////////////
  Player p1;        //Player 1
  Player p2;        //Player 2
  int teamNum;

  ////////////////////////////////////////////
  //Functions and stuff
  ////////////////////////////////////////////

}



public class Player {

  ////////////////////////////////////////////
  //Variable Declaration
  ////////////////////////////////////////////
  String fName;     //First Name
  String lName;     //Last Name

  ////////////////////////////////////////////
  //Functions and Stuff
  ////////////////////////////////////////////

}

For a database, would I want it something like this?

Relationships

Maybe I'm completely offbase with the whole idea. I'm not sure. If someone could let me know where I'm at, that would be great. All help's appreciated.

mandelbug
  • 1,548
  • 5
  • 20
  • 32
  • IMO, you could use some object-oriented database for that. – sybear Jul 02 '13 at 23:45
  • 2
    @Jari: it's a classical task for RDBMS – zerkms Jul 02 '13 at 23:47
  • If you use some PHP framework, such as Yii or Zend your tables will be `Model` (each one is a class). http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc – Eduardo M Jul 02 '13 at 23:56
  • if you know java oop and you worked with any kind of database before, there is no need to ask this questions. db is always same for all projects. if you are new to php oop you should learn some framework zend + some orm like doctrine. – pregmatch Jul 02 '13 at 23:56
  • 1
    are you saying you don't understand standard database data types? Like having problems setting up the architecture? Or do you mean you don't know how to connect, store and retrieve data from a database you believe to be correctly established? – Kai Qing Jul 03 '13 at 00:37
  • I get database data types, just architecture I guess. I can do everything else. I just don't know the logical way to setup a database for the purpose of storing "objects". For the example I provided with the Tournament, Team, and Player classes, how would I store a couple different Tournaments in the database, each with players, teams, etc. If I could get an example for these, I think I could figure it out, purely based on example. :) – mandelbug Jul 03 '13 at 03:07

2 Answers2

3

You could use an ORM library like Doctrine.

pdoherty926
  • 9,895
  • 4
  • 37
  • 68
2

If I understand this correctly, you're basically asking for advices on your data model ? This is pretty straightforward, what about this :

data model

Nothing fancy here :

  • A player has one team and a team has one to many players
  • A team has zero to many tournaments
  • A tournament has one (well, two actually) to many teams

Notice the many-many relationships between teams and tournaments. I'll let you add the relevant informations in your tables. For example you could add a date field in Tournaments_has_teams to keep track of tournaments of a team. To push the example a little further you could also have another table and one to many matches/sets in a tournament.

Once your database is setup, you can either create your own DAL or use an ORM to query it from your application. Note that you can't have a direct OOP/RDBMS mapping because of impedance mismatch (you can overcome this by using other technologies such as object-oriented database, like mentioned in the comments)

LMeyer
  • 2,570
  • 1
  • 24
  • 35
  • This helped immensely. Thanks! I guess I'll just have to try to find an OO database to work with (probably one mentioned above). I'm glad you confirmed my suspicion that there is no way to go directly from objects to a database nicely. I may try it the way you have it here, but I guess there's no point in making it more complicated than it has to be. Thanks again! – mandelbug Jul 03 '13 at 18:21
  • @JoshI Note that there's a reason OODBMS never had a breakthrough http://stackoverflow.com/questions/170649/why-have-object-oriented-databases-not-been-successful-yet Also make sure you validate the most helpful answer. – LMeyer Jul 03 '13 at 19:49