* Thus wrote Hardik Doshi:
> Hello Group,
> 
> I would like to know which one is the most appropriate
> way to implement the following scenario.
> 
> For example, I want to display a products catalogue of
> 100 products. I do have a base class of product which
> contains all the basic property of the product
> (Product title, product description, product price
> etc)and constructor basically pulls the information
> about the product from the DB based on the product
> identifier (Primary key). 
> 
> Now i have two ways to display catalogue.
> 
> 1. I can write only one query to pull all 100 products
> information and store product information to each
> product object (With out passing product id to the
> constructor) into the collection and later i iterate
> that collection to display product catalogue.
> (Advantage: less communication with database server
> and disadvantage: memory consumption is higher)
> 
> 2. I can initiate an individual product object by
> passing product id into the constructor and
> constructor will pull an individual product
> information from the DB and at the same time i can
> display it (Disadvantage: Lots of communication with
> database server and Advantage: memory consumption is
> less) If you think about inheritance then eventually
> this approach will have lots of database calls.
> 
> Please guide me as i am stuck up which way to go.

With these two options I can see why it is a tough choice. There is
another option you can take. Store the result handle of the query
from #1 into an object, and retrieve a row from the database on
demand.

abstract class DbIterator implements Iterator {

  /* for me only */
  private $current = 0;
  private $data    = false;
  private $handle = null;

  /* Force extending object to define this function */
  abstract protected function &getData($handle, $function);

  public function __construct($handle) {
    $this->handle = $handle;
  }

  /* Iterator Interface: */
  public function rewind() {
    $this->current = 0;
    db_seek($this->result, $this->current);
  }
  public function current() {
    return $this->data;
  }
  public function key() {
    return $this->current;
  }
  public function next() {
    $this->data = $this->getData($handle, 'db_fetch_assoc');
    if($this->data) {
      $this->current++;
    }
    return $this->data;
  }
  public function valid() {
    return (this->data !== false);
  }
}


class dbProductClass extends DbIterator {
  public $id;
  public $name;
  
  public function __construct($handle) {
    /* let the parent decide what to do with $handle */
    parent::__construct($handle)
  }

  /* and define this required function */
  protected function &getData($handle, $func) {
    $row = $func($handle);
    if ($row == false ) {
      return $row;
    }
    $this->$id = $row['id']

    if (empty($row['name']) {
      $this->name = 'Default Name';
    } else {
      $this->name = $row['name'];
    }
    // do any special stuff here..

    return &$this; 
  }

  public function tableName {
    return 'People';
  }

  /* this is a no no */
  public function __set($name, $value) {
    throw Execption("Attempt to write to readonly property");
  }



}

// then..
$result = db_query($sql);
$product_rows = new dbProductClass($result);

// then some more..
foreach($product_rows as $row ) {
  echo $row->field_name1;
  echo $row->field_name2;
}

This will keep communication down to a minimum and memory down to a
minimum usage as well. I hope this wasn't to much, and it is
completely untested.



Curt
-- 
First, let me assure you that this is not one of those shady pyramid schemes
you've been hearing about.  No, sir.  Our model is the trapezoid!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to