depth = $depth; $this->table = strtolower(get_class($this)); if (method_exists($this,'depends')) { $this->depends(); } if (!empty($id)) { $this->id = $id; $rows = $database->query("select * from ".$this->table." where id=?",$id); unset($rows[0]['id']); $this->attribs = $rows[0]; foreach ($rows[0] as $col=>$junk) { $this->fields[] = $col; } $this->loadChildren(); } else { $cols = $database->query('desc '.$this->table); foreach ($cols as $col) { if ($col['Field'] == 'id') { continue; } $this->fields[] = $col['Field']; } } } /*}}}*/ function save() {/*{{{*/ global $database; if (!empty($this->id)) { $query = "update ".$this->table." set "; $suffix = "where id=?"; } else { $query = "insert into ".$this->table." set "; $suffix = ""; } $queryargs = array(); foreach ($this->attribs as $attr=>$val) { if ($val !== NULL) { $query .= ' '.$attr."=?,"; $queryargs[] = $val; } } $query = substr($query,0,-1); $query .= $suffix; if (!empty($this->id)) { $queryargs[] = $this->id; } array_unshift($queryargs,$query); call_user_func_array(array($database,'query'),$queryargs); if (empty($this->id)) { $this->id = mysql_insert_id(); } } /*}}}*/ function update($vals) {/*{{{*/ foreach ($vals as $k=>$v) { if ($this->hasField($k) && array_search($k,$this->access) !== false) { $this->attribs[$k] = $v; } } } /*}}}*/ function delete() {/*{{{*/ global $database; if (!empty($this->id)) { $database->query("delete from ".$this->table." where id=?",$this->id); $this->id = NULL; } } function deleteChildren() { $attribs = get_object_vars($this); foreach ($attribs as $k=>$v) { if (preg_match('/^child_/',$k)) { foreach ($this->$k as $obj) { $obj->deleteChildren(); } } } $this->delete(); } /*}}}*/ function find() {/*{{{*/ global $database; $args = func_get_args(); $query = "select id from ".$this->table; if (isset($args[0])) { $query .= " where ".$args[0]; array_shift($args); array_unshift($args,$query); } $rows = call_user_func_array(array($database,'query'),$args); $return = array(); $class = get_class($this); foreach ($rows as $r) { $return[] = new $class($r['id'],$this->depth - 1); } return $return; } /*}}}*/ // relationship methods/*{{{*/ function has($model,$idcol='',$rank='') { if ($idcol == '') { $idcol = strtolower($model).'_id'; } $this->has[] = array('model'=>$model,'idcolumn'=>$idcol,'rank'=>$rank); } function belongsTo($model,$idcol='',$rank='') { if ($idcol == '') { $idcol = strtolower($model).'_id'; } $this->belongsto[] = array('model'=>$model,'idcolumn'=>$idcol,'rank'=>$rank); } function belongsToMany($model,$rank='') { $this->belongstomany[] = array('model'=>$model,'rank'=>$rank); } function hasField($field) { return (array_search($field,$this->fields) !== false); } /*}}}*/ // load children methods/*{{{*/ function loadChildren() { if ($this->depth == 0) { return; } foreach ($this->has as $h) { // each: array('model'=>'modelname','idcolumn'=>'idcolumn') $varname = 'child_'.$h['model']; $this->$varname = $this->loadHChildren(strtolower($h['model']),$h['idcolumn'],$this->id,$this->depth,$h['rank']); } foreach ($this->belongsto as $bt) { $varname = 'parent_'.$bt['model']; $this->$varname = $this->loadBTChild(strtolower($bt['model']),$bt['idcolumn'],$this->depth,$bt['rank']); } foreach ($this->belongstomany as $btm) { $varname = 'parent_'.$btm['model']; $this->$varname = loadBTMChildren(strtolower($btm),$this->depth,$btm['rank']); } } function loadHChildren($model,$col,$val,$depth,$rank) { global $database; $return = array(); $ids = $database->query("select id from ".$model." where ".$col."=?".($rank != '' ? ' order by '.$rank : ''),$val); foreach ($ids as $id) { $return[] = new $model($id['id'],$depth - 1); } return $return; } function loadBTChild($model,$col,$depth,$rank) { global $database; $parents = $database->query("select id from ".$model." where id=?".($rank != '' ? ' order by '.$rank : ''),$this->attribs[$col]); return new $model($parents[0]['id'],$depth - 1); } function loadBTMChildren($model,$depth,$rank) { global $database; $parents = $database->query("select * from ".$model."_".get_class($this)."_link where ".get_class($this)."_id=?".($rank != '' ? ' order by '.$rank : ''),$this->attribs['id']); $return = array(); foreach($parents as $p) { $return[] = new $model($p[$model.'_id'],$depth - 1); } return $return; }/*}}}*/ } ?>