Skip to content

Instantly share code, notes, and snippets.

@zspencer
Created July 10, 2012 23:48
Show Gist options
  • Save zspencer/3087002 to your computer and use it in GitHub Desktop.
Save zspencer/3087002 to your computer and use it in GitHub Desktop.
I found this PHP implementation of 'active record' I did nearly 7 years ago. Below you see an implementation of a model.
<?php
class active_record
{
function table_name()
{
return get_class($this);
}
function conf($conf=NULL)
{
if($conf!==NULL)
{
$this->conf=$conf;
}
elseif(!isset($this->conf) OR $this->conf==null)
{
$this->conf='active_record';
}
return $this->conf;
}
function __wakeup()
{
$this->__CONSTRUCT($this->table_name(),$this->db,$this->conf());
}
function __CONSTRUCT($table_name, $db=NULL,$conf=NULL)
{
$conf=$this->conf($conf);
$db=$this->get_db($db);
global $class_definitions;
if(!isset($class_definitions[$table_name]) OR count($class_definitions[$table_name])==0)
{
$query='DESCRIBE '.$db->real_escape_string($GLOBALS['config']->$conf->table_prefix.$table_name);
$result=$db->query($query);
while($row=$db->fetch_assoc($result))
{
$field=$row['Field'];
$GLOBALS['class_definitions'][$table_name][$field]=$row['Default'];
if(!isset($this->$field))
{
$default=$row['Default'];
eval("\$this->$field='$default';");
}
}
unset($result);
}
}
function load($identification=NULL, $identification_field=NULL, $recursive=FALSE, $db=NULL, $search=NULL)
{
global $class_definitions;
$db=$this->get_db($db);
$conf=$this->conf();
if($identification===NULL AND $identification_field===NULL)
{
$identification_string='';
}
else
{
$identification_string=$this->get_identification_string($identification, $identification_field, $db, $search);
}
$query='SELECT * FROM '.$GLOBALS['config']->$conf->table_prefix.$this->table_name().$identification_string;
$result=$db->query($query);
$classes=array();
if($db->num_rows($result)>0)
{
while($row=$db->fetch_assoc($result))
{
if(array_key_exists($this->primary_key_name(), $class_definitions[$this->table_name()]))
{
$classes[$row[$this->primary_key_name()]]=$this->assign_vars_from_row($row, NULL, $recursive, $db);
}
else
{
$classes[]=$this->assign_vars_from_row($row, NULL, $recursive, $db);
}
}//Done looping through the returned row
}//done executing the code for multiple rows
else
{//No rows were returned
$this->generate();
$classes[]=$this;
}
return $classes;
}//done withthe loading function
function generate($db=NULL)
{
$db=$this->get_db($db);
global $class_definitions;
foreach($class_definitions[$this->table_name()] as $field=>$value)
{
eval("\$this->$field=\$value;");
}
}
function assign_vars_from_row($row, $class=NULL, $recursive=NULL, $db=NULL)
{
$db=$this->get_db($db);
$conf=$this->conf();
global $retrieved_classes;
if($class===NULL)
{
$table_name=$this->table_name();
eval("\$class = new $table_name;");
}
foreach($row as $key=>$value)
{
if(substr($key, strlen($key)-strlen($GLOBALS['config']->$conf->foreign_key_suffix))==$GLOBALS['config']->$conf->foreign_key_suffix AND $key!=$GLOBALS['config']->$conf->primary_key AND $recursive===TRUE)
{
$new_class_name=substr($key, 0, strlen($key)-strlen($GLOBALS['config']->$conf->foreign_key_suffix));
if(!class_exists($new_class_name))
{
$this->new_class($new_class_name, $db);
}
if(isset($retrieved_classes[$new_class_name][$value]))
{
eval("\$class->$new_class_name".$GLOBALS['config']->$conf->foreign_key_suffix."=\$retrieved_classes['$new_class_name']['$value'];");
}
else
{
$new_class = new $new_class_name;
if($GLOBALS['config']->$conf->primary_key_uses_table_name)
{
$primary_key=$new_class_name.$GLOBALS['config']->$conf->primary_key;
}
else
{
$primary_key=$GLOBALS['config']->$conf->primary_key;
}
eval("\$new_class->load('$value','".$this->primary_key_name()."', FALSE, \$db);");
$retrieved_classes[$new_class_name][$value]=$new_class;
eval("\$class->$key=\$value;");
eval("\$class->$new_class_name".$GLOBALS['config']->$conf->foreign_key_suffix."=\$new_class;");
}
}
else
{
eval("\$class->$key=\"$value\";");
}
}
//$class->load_relatives($db);
return $class;
}//Done assigning Vars to Rows
function new_class($new_class_name, $db=NULL)
{
$db=$this->get_db($db);
if(file_exists($GLOBALS['config']->models_path.$new_class_name.'.class.php') AND !class_exists($new_class_name))
{
$snappad->load_model($new_class_name);
}
elseif(!class_exists($new_class_name))
{
eval("class $new_class_name extends active_record
{
function __CONSTRUCT()
{
parent::__CONSTRUCT('$new_class_name');
}
}
");
}
}
function save($recursive=FALSE, $db=NULL)
{
$db=$this->get_db($db);
$conf=$this->conf();
global $class_definitions;
$values='';
if(!isset($class_definitions[$this->table_name()]))
{
$this->__CONSTRUCT($this->table_name(),$db);
}
foreach($class_definitions[$this->table_name()] as $field=>$default)
{
if(array_key_exists($field, $this))
{
eval("\$value=\$this->$field;");
//echo $field.'='.$value.'<br /><br />';
if(is_object($value))
{
//echo 'its an object!';
if($recursive===TRUE)
{
$value->save($recursive, $db);
}
$values.=$db->escape_string($field).'="'.$db->escape_string($foreign_key).'", ';
}
elseif($field===$this->primary_key_name())
{
if($value!=NULL)
{
$identification_string ='WHERE '.$db->escape_string($field).'="'.$db->escape_string($value).'"';
$values.=$db->escape_string($field).'="'.$db->escape_string($value).'", ';
}
}
else
{
$values.=$db->escape_string($field).'="'.$db->escape_string($value).'", ';
}
}
}
$values=trim($values,', ');
$values.=' ';
if(isset($identification_string) AND trim($identification_string!=''))
{
$query='UPDATE '.$GLOBALS['config']->$conf->table_prefix.$this->table_name().' SET '.$values.$identification_string;
}
else
{
$query='INSERT INTO '.$GLOBALS['config']->$conf->table_prefix.$this->table_name().' SET '.$values;
}
$db->query($query);
if($db->insert_id()!=0)
{
$primary_key_name=$this->primary_key_name();
eval("\$this->\$primary_key_name=\$db->insert_id();");
}
}
function destroy($identification=NULL, $identification_field=NULL, $db=NULL)
{
$db=$this->get_db($db);
$conf=$this->conf();
$identification_string=$this->get_identification_string($identification, $identification_field);
if($identification_string!='')
{
$query='DELETE FROM '.$GLOBALS['config']->$conf->table_prefix.$this->table_name().$identification_string;
$db->query($query);
}
}
function has_one($table, $override_fk=NULL)
{
global $class_definitions;
if($override_fk===NULL)
{
$override_fk=$table;
}
$this->has_one[$table]=$override_fk;
}
function has_many($table, $override_fk=NULL)
{
global $class_definitions;
if($override_fk===NULL)
{
$override_fk=$this->table_name();
}
$this->has_many[$table]=$override_fk;
}
function load_tables($db=NULL)
{
$db=$this->get_db($db);
$conf=$this->conf();
global $class_definitions;
$query='SHOW TABLES';
$result=$db->query($query);
while($row=$db->fetch_array($result))
{
if(!isset($class_definitions[substr($row[0],strlen($GLOBALS['config']->$conf->table_prefix))]))
{
$class_definitions[substr($row[0],strlen($GLOBALS['config']->$conf->table_prefix))]='';
}
}
}
function load_relatives($db=NULL)
{
$db=$this->get_db($db);
$conf=$this->conf();
if(isset($this->has_one))
{
foreach($this->has_one as $table=>$join_fk)
{
if(!class_exists($table))
{
$this->new_class($table);
}
$temp_class=new $table;
eval("\$value=\$this->$join_fk".$GLOBALS['config']->$conf->foreign_key_suffix.";");
$temp_class=$temp_class->load($value, $this->primary_key_name(), FALSE, $db);
eval("\$this->$table=\$temp_class;");
}
}
if(isset($this->has_many))
{
foreach($this->has_many as $table=>$join_fk)
{
if(!class_exists($table))
{
$this->new_class($table, $db);
}
$temp_class=new $table;
if($GLOBALS['config']->$conf->primary_key_uses_table_name)
{
$primary_key=$this->table_name().$GLOBALS['config']->$conf->primary_key;
}
else
{
$primary_key=$GLOBALS['config']->$conf->primary_key;
}
eval("\$value=\$this->$primary_key;");
$temp_class=$temp_class->load($value, $join_fk.$GLOBALS['config']->$conf->foreign_key_suffix, FALSE, $db);
eval("\$this->$table=\$temp_class;");
}
}
}
function get_identification_string($identification=NULL, $identification_field=NULL, $db=NULL, $search=NULL)
{
$db=$this->get_db($db);
$identification_string=' WHERE ';
if(is_array($identification) AND count($identification)>0)
{
foreach($identification as $ident_field=>$ident_val)
{
$identification_string.=$db->escape_string($ident_field);
if(isset($identification_field[$ident_field]))
{
if($identification_field[$ident_field]=='like')
{
$identification_string.=' LIKE "%'.$db->escape_string($ident_val).'%" AND ';
}
else
{
$identification_string.=$identification_field[$ident_field].' "'.$db->escape_string($ident_val).'" AND ';
}
}
elseif($search===NULL)
{
$identification_string.='="'.$db->escape_string($ident_val).'" AND ';
}
else
{
$identification_string.=' LIKE "%'.$db->escape_string($ident_val).'%" AND ';
}
}
$identification_string = rtrim($identification_string, ' AND');
}
else
{
if($identification===NULL)
{
eval("\$identification=\$this->".$this->primary_key_name().";");
}
if($identification_field==NULL)
{
$identification_field=$this->primary_key_name();
}
if($search===NULL)
{
$identification_string.=$db->escape_string($identification_field).'="'.$db->escape_string($identification).'"';
}
else
{
$identification_string.=$db->escape_string($identification_field).' LIKE "%'.$db->escape_string($identification).'%"';
}
}
if($identification_string!=' WHERE')
{
return $identification_string;
}
}
function primary_key_name($table=NULL)
{
$conf=$this->conf();
if($table===NULL)
{
$table=$this->table_name();
}
if(isset($this->primary_key_name))
{
$primary_key_name=$this->primary_key_name;
}
elseif($GLOBALS['config']->$conf->primary_key_uses_table_name)
{
$primary_key_name=$table.$GLOBALS['config']->$conf->primary_key;
}
else
{
$primary_key_name=$GLOBALS['config']->$conf->primary_key;
}
return $primary_key_name;
}
function is_unique($identification=NULL, $identification_field=NULL, $db=NULL)
{
$db=$this->get_db($db);
$query='SELECT * FROM '.$this->table_name().$this->get_identification_string($identification,$identification_field, $db);
$result = $db->query($query);
if($db->num_rows($result)>0)
{
return false;
}
else
{
return true;
}
}
function get_db($db=NULL)
{
if($db!==NULL AND is_object($db) AND $db->link!=NULL)
{
$this->db=&$db;
}
if(!isset($this->db) OR $this->db->link()==NULL)
{
$this->db=&$GLOBALS['db'];
}
return $this->db;
}
function search($identification=NULL, $identification_field=NULL, $db=NULL)
{
$db=$this->get_db($db);
$query='SELECT * FROM '.$this->table_name().$this->get_identification_string($identification,$identification_field,$db,TRUE);
}
}
?>
<?
$GLOBALS['snappad']->load_module('active_record');
class event extends active_record
{
function __CONSTRUCT()
{
parent::__CONSTRUCT('event',$GLOBALS['db']);
}
function load($identification=NULL, $identification_field=NULL, $recursive=FALSE, $db=NULL, $search=NULL)
{
$events = parent::load($identification,$identification_field,$recursive,$db,$search);
foreach($events as &$event)
{
if(isset($event->id))
{
$event->load_usage_dates();
$event->load_venues();
}
}
return $events;
}
function destroy($identification=NULL, $identification_field=NULL, $db=NULL)
{
$GLOBALS['snappad']->load_model('event_date');
$GLOBALS['snappad']->load_model('event_venue_link');
if($identification===NULL AND $identification_field===NULL)
{
$event_date = new event_date;
$event_date->destroy($this->id,'event_id');
$event_venue_links = new event_venue_link;
$event_venue_links->destroy($this->id,'event_id');
}
parent::destroy($identification,$identification_field,$db);
}
function load_usage_dates()
{
$GLOBALS['snappad']->load_model('event_date');
$usage_dates = new event_date;
$this->usage_dates = $usage_dates->load($this->id,'event_id');
if(isset($this->usage_dates))
{
return true;
}
else
{
return false;
}
}
function load_venues()
{
$GLOBALS['snappad']->load_model('event_venue_link');
$GLOBALS['snappad']->load_model('venue');
$event_venue_links = new event_venue_link;
$event_venue_links = $event_venue_links->load($this->id,'event_id');
foreach($event_venue_links as $event_venue_link)
{
$venue = new venue;
$venue = current($venue->load($event_venue_link->venue_id,'id'));
$this->venues[$venue->id]=$venue;
}
if(isset($this->venues))
{
return true;
}
else
{
return false;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment