* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ require_once("Database.class.php"); class BaseVO { var $table = null; var $className = __CLASS__; var $id = null; /** * Loads an entry given it's id. */ function load($id){ global $db; $this->loadFromArray($db->queryArray("SELECT * FROM ".$this->table." WHERE id='".$id."';")); } function save(){ global $db; // Get the fields. $rs =& mysql_query("DESCRIBE ".$this->table.";"); while($t = mysql_fetch_assoc($rs)){ $fields[$t["Field"]] = $this->$t["Field"]; } $db->saveArray($this->table,$fields); } /** * Loads the object from an array, possibly from an SQL-ResultSet. */ function loadFromArray($arr){ if(count($arr)<2) return; foreach($arr as $k => $v){ $this->$k = $v; } } function setForeign($className,$key,$value){ $cacheName = "_".$key; if(is_numeric($value)){ $this->$key =& $value; $this->$cacheName = null; }else{ $this->$key = $value->id; $this->$cacheName =& $value; } } function getForeign($className,$key){ $cacheName = "_".$key; if(empty($this->$cacheName)){ $o =& new $className(); $o->load($this->$key); $this->$cacheName =& $o; } return $this->$cacheName; } function &getMany($className,$options){ global $db; $data =& $db->queryAllArray("SELECT * FROM ".(empty($options['table'])?$className."s":$options['table'])." WHERE ".(empty($options['foreign'])?$this->className:$options['foreign'])."='".$this->id."';"); $res = array(); foreach($data as $d){ $o =& new $className(); $o->loadFromArray($d); $res[]=& $o; } return $res; } } ?>