View Single Post
 
Old March 28th, 2006, 07:09 AM
moondrake moondrake is offline
Registered User
 
Join Date: Oct 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by soccer022483
Has anyone rewritten the GenericObject class to work with the database class instead of the sql class? If not, could someone help me rewrite it?
Yeah, just done that tonight. If I've done it correctly, using the Database class simplifies GenericObject (and GenericObjectCollection) quite a bit.

My website is in a state of disrepair, otherwise I'd post a link rather than dump it here. I'm a part-time programmer and still need to test this out, but it seems to work... Anyone want to suggest improvements I'm all ears (or eyes in this case).

Anyway, here is how I've rewritten the GenericObject Classes to make use of the Database layer in chap 8. Hope it helps...

GenericObject
Code:
<?

require_once('class.Database.phpm');

class GenericObject {

    # Member Variables

    private $id;
    private $table_name;

    private $database_fields;
    private $loaded;
    private $modified_fields;

    # Methods

    public function Reload() {
        try {
            $db = Database::instance();
        } catch (Exception $e) {
            // no point continuing
            die("Unable to connect to the database");
        }

        $id = $this->id;
        $table_name = $this->table_name;
        $db->select("SELECT * FROM \"$table_name\" WHERE id='$id'");
        $result_fields = $db->select("SELECT * FROM \"$table_name\" WHERE id='$id'");
        $this->database_fields = $result_fields;
        $this->loaded = 1;
        if (sizeof($this->modified_fields) > 0) {
            foreach ($this->modified_fields as $key => $value) {
                $this->modified_fields[$key] = false;
            }
        }
    }

    private function Load() {
        $this->Reload();
        $this->loaded = 1;
    }

    public function ForceLoaded() {
        $this->loaded = 1;
    }

    public function GetField($field) {
        if ($this->loaded == 0) {
            $this->Load();
        }
        return $this->database_fields[$field];
    }

    public function GetAllFields() {
        if ($this->loaded == 0) {
            $this->Load();
        }
        return($this->database_fields);
    }

    public function GetID() {
        return $this->id;
    }

    public function Initialize($table_name, $tuple_id = "") {
        $this->table_name = $table_name;
        $this->id = $tuple_id;
    }

    public function SetField($field, $value) {
        if ($this->loaded == 0) {
            if ($this->id) {
                $this->Load();
            }
        }
        $this->database_fields[$field] = $value;
        $this->modified = 1;
        $this->modified_fields[$field] = true;
    }

    public function Destroy() {
        $id = $this->id;
        $table_name = $this->table_name;
        if ($id) {
            $db = Database::instance();
            $stmt = "DELETE FROM \"" . $table_name . "\" WHERE id='" . $id . "'";
            $db->delete($stmt);
        }
    }

    public function Save() {
        $id = $this->id;
        $table_name = $this->table_name;
        $database_fields = $this->database_fields;
        $db = Database::instance();

        if (!$id) {
            # then the id hasn't been set so this is a new record, and hence set loaded to false
            $this->loaded = 0;
        }

        if ($this->loaded == 0) {
            # assume this is a new entity and therefor use the insert function
            if ($this->id = $db->insert($table_name, $database_fields)) {
                $this->loaded = 1;
                return true;
            }
        } else {
            # then an existing record is being updated.  Build an array of updated fields.

            $sWhere = "id = " . $this->id;
            $table_name = $this->table_name;
            $arUpdates = array(); // key -> value array of the fields to be updated
            $db = Database::instance();

            foreach ($this->database_fields as $key => $value) {
                if ($this->modified_fields[$key] == true) {
                    $arUpdates[$key] = $value;
                }
            }

            # now call the DB object's update method, using $sWhere to limit the effect
            if ($db->update($table_name, $arUpdates, $sWhere)) {
                return true;
            }
        }

        # if we've got this far, then either the insert or update methods failed so...
        return false;
    }
}
GenericObjectCollection
Code:
<?
    class GenericObjectCollection {

        # Member Variables
        var $table_name;
        var $class_name;

        var $items_per_page;
        var $item_count = 0;

        var $id_array;

        var $obj_array;

        function __construct($table_name, $class_name) {
            $this->table_name = $table_name;
            $this->class_name = $class_name;
        }

        function AddTuple($id) {
            if (!$this->id_array) {
                $this->id_array = array();
            };
            array_push($this->id_array, $id);
            $this->item_count = sizeof($this->id_array);
            }

        function SetPageSize($items_per_page) {
            $this->items_per_page = $items_per_page;
        }

        function GetItemCount() {
            return $this->item_count;
        }

        function GetNumPages() {
            return(ceil($this->item_count / $this->items_per_page));
        }

        function _GetCommaSeparatedIDList($start_lim = 0, $end_lim = -1) {
            $s = "";
            if ($end_lim == -1) {
                $end_lim = sizeof($this->id_array)-1;
            };
            for ($i=$start_lim; $i<=$end_lim; $i++) {
                if (is_numeric($this->id_array[$i])) {
                    $s = $s . $this->id_array[$i] . ",";
                };
            };
            $s = substr($s, 0, strlen($s) - 1);
            return $s;
        }

        function _GetIndexFromTupleID($tuple_id) {
            $index = -1;
            for ($i=0; $i<=sizeof($this->id_array)-1; $i++) {
                if ($this->id_array[$i] == $tuple_id) {
                    $index = $i;
                };
            };
            return $index;
        }

        function PopulateObjectArray($page_num = 0) {
            $items_per_page = $this->items_per_page;
            if ($this->item_count > 0) {
                if ($page_num > 0) {
                    $start_lim = ($items_per_page * ($page_num - 1));
                    $end_lim = ($start_lim + $items_per_page) - 1;
                    if ($end_lim > ($this->item_count-1)) {
                        $end_lim = $this->item_count - 1;
                    };
                    $stmt = "SELECT * FROM " . $this->table_name . " WHERE id IN (" . $this->_GetCommaSeparatedIDList($start_lim, $end_lim). ")";
                } else {
                    $stmt = "SELECT * FROM " . $this->table_name . " WHERE id IN (" . $this->_GetCommaSeparatedIDList(). ")";
                };
                # Perform SQL query 
                $db = Database::instance();
                $result_rows = $db->getAll($stmt);

                for ($i=0; $i<=sizeof($result_rows)-1; $i++) {
                    $this_row = $result_rows[$i];
                    $this_db_row_id = $this_row["id"];
                    $this_index = $this->_GetIndexFromTupleID($this_db_row_id);
                    if ($this_index >= 0) {
                        $refObjArrayIndexObj = &$this->obj_array[$this_index];
                        $s = "\$refObjArrayIndexObj = new " . $this->class_name . "(" . $this_db_row_id . ");";
                        eval($s);
                        $refObjArrayIndexObj->ForceLoaded();
                        foreach ($this_row as $key => $value) {
                            if (!(is_numeric($key))) {
                                $refObjArrayIndexObj->SetField($key, $value);
                            };
                        };
                    };
                };
            };
        }

        function RetrievePopulatedObjects($page_num = 0) {
            if ($page_num > 0) {
                $items_per_page = $this->items_per_page;
                # Calculate start and end limits from page number.
                $start_lim = ($items_per_page * ($page_num - 1));
                $end_lim = ($start_lim + $items_per_page) - 1;
                    if ($end_lim > ($this->item_count-1)) {
                        $end_lim = $this->item_count - 1;
                    };        
            } else {
                $start_lim = 0;
                $end_lim = $this->item_count - 1;
            };
            $return_array = array();
            $counter = 0;
            for ($i=$start_lim; $i<=$end_lim; $i++) {
                $return_array[$counter] = $this->obj_array[$i];
                $counter++;
            };
            return($return_array);
        }
  }
?>