Jump to content


Photo

Conceptual question about OOP and SQL


  • Please log in to reply
3 replies to this topic

#1 JarWithLid

JarWithLid
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 11 September 2006 - 05:04 AM

Here is my question:

Say I have the objects below. Supose an ImageSet object is created, named $fauxSet, that contains 200 images. When you call $fauxSet->getImages() you are creating an array of 200 Image objects. Awesome. So nice. But is that considered horrible as it will hit the mySql server with 201 queries when you could get the same info, without creating the objects, with just 1? The latter would have drawbacks: Sacraficing code reuse principles of OOP, no longer having the ability to use someFunction() on those objects, etc.

Opinions / Suggestoins?

class Image {
  public $id;
  public $setid;
  public $name;
  public $description;
  public $file;

  public function __construct($id) {
    // Query images table, fill member variables
  }

  public function someFunction () {
    // Some function you can perform with this object
  }
}

class ImageSet {
  public $id;
  public $name;
  public $description;

  public function __construct($id) {
    // Query image_sets table, fill member variables
  }

  // This function returns an array of the Image objects in this ImageSet
  public function getImages () {
    $imgAr = array(); // What we will return
    $images = new mySql("SELECT id FROM images WHERE setid = $this->id"); // Gets all of the image ids from database
    
    while ($images->nextRow()) {
      $imgAr[] = new Image($images->id);
    }

    return $imgAr;
  }
}



#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 11 September 2006 - 08:01 AM

There's always a tradeoff between abstraction and efficiency.

In this case, you can have a option or method for your class that says "I will be reading all images, read them in to a cache now".  Then your getImages() can check the cache and use that data if possible, with an sql query as a fallback.  It's some loss of abstraction, but you can't have everything :)

#3 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 11 September 2006 - 08:17 AM

As above, and besides creating a mySql object for all the images available surely isn't that overtly non-abstract. Instead of using a Row Data Gateway pattern, you are using a Table Data Gateway (use this instead of an array for all images)... or even Object Relational Mapping. If you still want each image to retain image specific info in it's own pattern, you should create a new object for that image upon selecting the individual image.

Something like:

<?php

$db = new DB(/* connection gubbins */);

$db->exec('SELECT * FROM `images`');

/* this is the table data gateway */
$ImageData = new ImageDataGateWay($db);

while ($image = $ImageData->fetchImage())
{
    echo $image->name . '<br />' . chr(10);
}

?>


#4 JarWithLid

JarWithLid
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 September 2006 - 01:43 AM

Do you have a recomendation for a book on said design patterns?

Thanks,
Adam




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users