query=$GLOBALS[$this->factory]->returnObject('pvl_query'); } # End function pvl_DAO() # Finish setting up the object and grab the info from the database function load($id, $table, $primary='') { // my_print_r($GLOBALS[$this->factory]); // my_print_r($this); $this->query=&$GLOBALS[$this->factory]->returnObject('pvl_query'); $this->query->setupObject(); my_print_r($this); # Let's first setup some vars $this->table=$table; $this->id=$id; if($primary=='') { $this->primary=$this->findPrimary($this->table); } else { $this->primary=$primary; } $SQL='SELECT * FROM '.$this->table.' WHERE '.$this->primary.' = '.$this->id; $this->query->dbQuery($SQL); while($row=$this->query->dbFetchArray('object')) { $this->data=$row; } } # End function load() # Change or set a value in the object function set($key, $val) { if($this->checkKeyExistence($key)==true) { $this->data->$key=$val; return true; } else { return false; } } # End function set() # Get a value from the object function get($key) { if($this->checkKeyExistence($key)==true) { return $this->data->$key; } else { return false; } } function checkKeyExistence($key) { if(array_key_exists($key, $this->data)) { return true; } else { return false; } } # End function get() # Get the primary key for the table if one is not provided function findPrimary() { $SQL='SHOW COLUMNS FROM '.$this->table; $this->query->dbQuery($SQL); while ($row=$this->query->dbFetchArray('object')) { if($row->Key=='PRI') { return $row->Field; } } } # End function findPrimary() # Write the data back to the datasource function writeData() { $SQL='UPDATE '.$this->table.' SET '; foreach($this->data as $k => $v) { if($k==$this->primary) { continue; } $SQL.="$k = '$v', "; } $SQL.='WHERE '.$this->primary.' = '.$this->id; $SQL=str_replace(', WHERE', ' WHERE', $SQL); echo $SQL."\n"; $this->query->dbQuery($SQL); echo mysql_error()."\n"; } # End funciton writeData() } # End class pvl_DAO ?>