Implement autoloading

develop
Sven Slootweg 12 years ago
parent a8937630e3
commit d3c970a551

@ -20,6 +20,7 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
public $table_name = "";
public $query_cache = 60;
public $id_field = "Id";
public $autoloading = true;
public $prototype = array();
public $prototype_render = array();
@ -34,6 +35,36 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
$this->EventConstructed();
}
public function __get($name)
{
if($name[0] == "s" || $name[0] == "u")
{
$actual_name = substr($name, 1);
$found = false;
foreach($this->prototype as $type => $dataset)
{
if(isset($dataset[$actual_name]))
{
$found = true;
$found_type = $type;
$found_field = $dataset[$actual_name];
}
}
if($found === false)
{
$classname = get_class($this);
throw new PrototypeException("The {$actual_name} variable was not found in the prototype of the {$classname} class.");
}
$this->SetField($found_type, $actual_name, $found_field);
return $this->$name;
}
}
public function RefreshData()
{
$this->PurgeCache();
@ -105,10 +136,13 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
$this->uData = $uDataSource;
if($this->autoloading === false)
{
foreach($this->prototype as $type => $dataset)
{
$this->BindDataset($type, $dataset, $defaultable);
}
}
$this->sFound = true;
}
@ -126,6 +160,20 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
{
foreach($dataset as $variable_name => $column_name)
{
$this->SetField($type, $variable_name, $column_name);
}
}
else
{
$classname = get_class($this);
throw new Exception("Invalid dataset passed on to {$classname}.BindDataset.");
}
}
public function SetField($type, $variable_name, $column_name)
{
global $cphp_class_map;
if(!isset($this->uData[$column_name]))
{
throw new Exception("The column name {$column_name} was not found in the resultset - ensure the prototype corresponds to the table schema.");
@ -178,17 +226,10 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
$value = new $class_name($original_value);
}
catch (NotFoundException $e)
{
if(is_array($defaultable) && in_array($variable_name, $defaultable))
{
$value = new $class_name(0);
}
else
{
$e->field = $variable_name;
throw $e;
}
}
$variable_type = CPHP_VARIABLE_SAFE;
$found = true;
}
@ -211,13 +252,6 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
$variable_name_unsafe = "u" . $variable_name;
$this->$variable_name_unsafe = $original_value;
}
}
else
{
$classname = get_class($this);
throw new Exception("Invalid dataset passed on to {$classname}.BindDataset.");
}
}
public function FillDefaults()
{

Loading…
Cancel
Save