Advertisement

04.14.2005 at 10:02AM PDT, ID: 21389103
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.4

Bridging the object/relational divide

Asked by Squinky in PHP Scripting Language

Tags:

OK, here's a question that should get you thinking. It's been bothering me for quite some time. It's not urgent, but it's definitely worth 500 points for a good answer!

In a typical abstracted set of objects, it's common to have many instances grouped under some other instance, for example passengers on a flight, subscribers in a mailing list etc. These are each represented by independent objects that can look after their own data. So my passenger class might be something like (all pseudocode):

class passenger {
  var name;
  var seatnum; //etc
  var flightid;
  function passenger($id=0) { //constructor
    if (id > 0 ) $this->get($id);
  }
  function get($id) {
    query(select * from passengers where id = $id) //Query database to get passenger details
  }
  function update() {
   //Write altered passenger data back to db
  }
}

and my flight might be:

class flight {
  var $start
  var $destination
  var $time
  var $passengers
  function flight() { //constructor
    $this->passengers = array();
  }
  function getpassengers() {
    $passengers = querydatabase(select id from passengers where flightid = $this->id)
    foreach ($passengers as $passengerid)
      $this->passengers[] = new passenger($passengerid); //get all the passenger objects for this flight
  }
  //flight storage skipped
}

this is all nice and encapsulated - passengers and flight are self-sufficient in terms of data storage, neither needs to know about the other's internals.

Now the problem arises. Say I have 100 passengers - I'm going to be doing 101 database queries to get their data - a serious performance problem.
Getting all the data could be done in 1 query, but where to put it? A passenger shouldn't have to know about other passengers, and a flight shouldn't have to know all the details of each passenger. I've seen suggestions to use a factory class that creates multiple passengers, but I can't see how to do that in an object-clean way. This would work:

class passengerfactory {
  static function makepassengers($flightid) {
    $passengerarray = array();
    $passengers = query(SELECT * from passenger where flightid = $flightid)
    foreach($passengers as $passenger) //pretend that $passenger contains a complete record
      $p = new passenger;
      $p->flightid = $flightid;
      $p->name = $passenger['name'];
      $p->seatno = $passenger['seatno'];
  }
  return $passengerarray;
}

but it means that this class has unnecessary inside knowledge of the passenger class and its database table, and if I change the passenger class then I have to update this one too, breaking my encapsulation. That's not a big deal in this simple example, but it can be in bigger hierarchies, and it's the principle of the thing I'm trying to nail down.

If I made the factory class a subclass of passenger then it would at least keep the data private by inheriting it, but it's still not a clean definition - the list of passengers is not a specialisation of a passenger (as for example flight crew might be).

I'm sure that a good answer to this is in a design patterns book somewhere, but I've not been able to find one. Any ideas?

I've got another question like this too, but that can wait...Start Free Trial
 
 
Loading Advertisement...
 
[+][-]04.14.2005 at 02:43PM PDT, ID: 13786340

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.14.2005 at 05:56PM PDT, ID: 13787348

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: PHP Scripting Language
Tags: divide
Sign Up Now!
Solution Provided By: matt_mcswain
Participating Experts: 4
Solution Grade: A
 
 
[+][-]04.14.2005 at 09:58PM PDT, ID: 13788131

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.15.2005 at 12:10AM PDT, ID: 13788583

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.15.2005 at 01:27AM PDT, ID: 13788901

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.21.2005 at 04:26AM PDT, ID: 13832456

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.15.2005 at 10:18AM PDT, ID: 14006553

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.16.2005 at 12:48AM PDT, ID: 14008538

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.25.2005 at 09:35AM PDT, ID: 14954559

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-42