ok... ive just started working on my nested sets implimentation, and allready i think ive run into a problem. currently i have this...

    class tree
    {

    private $db;
    private $table;
    private $result;

/* {{{ construct / deconstruct */

    function __construct($table=CHLOE_DBTREETABLE) {
        try {
            $this->table = $table;
            $this->db = pg_connect(
		"host=".CHLOE_DBHOST." 
		dbname=".CHLOE_DBNAME." 
		user=".CHLOE_DBUSER." 
		password=".CHLOE_DBPASS
		);

        } catch(exception $e) {
            throw new exception("connection to database failed : ".pg_last_error());

        }

    }
    function __deconstruct() {
        pg_close($this->db);    

    }

/* }}} */
/* {{{ helpers */

    public function result() {
        return pg_fetch_assoc($this->result);

    }

/* }}} */
/* {{{ fetch methods */

    public function gettree() {
        $query = "
                    SELECT id,parent,lbound,rbound,lvl,(data).name AS name
                    FROM %s
                    ORDER BY lbound
                ";

        if ($this->result = pg_query($this->db,sprintf($query,$this->table))) {
            return 1;

        }
        return 0;

    }
    public function getpath() {}
    public function getnode() {}
    public function getpreviousoftype() {}
    public function getnextoftype() {}

/* }}} */

that is just part of it, but its all ive done so far. anyway, currently, for testing im running this client code...

<?php

try {
    $tree = new tree();

} catch (exception $e) {
    echo $e->getmessage();

}
if ($tree->gettree()) {
    while ($row = $tree->result()) {
        echo $row['id']." ";
        echo $row['parent']." ";
        echo $row['lbound']." ";
        echo $row['rbound']." ";
        echo $row['lvl']." ";
        echo $row['name']."<br />";
    }
}

?>

now.... my main concern is the use of that there result() method. if while i was looping through the result of a call to $tree->gettree() as above, i decided i wanted to call another method (for instance $tree->getpath()), this would ruin the current result. i cant actually think of a reason at this point in time that would see me needing to call another method inside one of these loops, but well, we cant all see into the future.

i really have no idea about OOP, but im trying here. im sure there is a better way of implimenting this without having the gettree() method simply return a resource.

can anyone give me any pointers here? or has anyone any suggestions on how this could better be achieved. or am i infact doing it correctly?

    ok... just to let you know. i do realise i could just change the result method to...

    public function result($result) {
        return pg_fetch_assoc($result);
    
    }
    

    and then use it like... (of course i would also need to change the gettree() method to actually return the result)

    if ($structure = $tree->gettree()) {
        while ($row = $tree->result($structure)) {
            echo "blah";
        }
    }
    

    but that just doesn't seem as neat. is that how it really should be done?

      ok... one more thing. i have php5 compiled with SPL. im wondering if i could use some sort of iterator here? im looking at a few tutorials atm, but if anyone could lend a hand, would be great.

        Write a Reply...