Jump to content

Conceptual question about OOP and SQL


JarWithLid

Recommended Posts

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?

[code]
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;
  }
}
[/code]

Link to comment
Share on other sites

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 :)
Link to comment
Share on other sites

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:

[code]<?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);
}

?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.