[eluser]Bryan Zera[/eluser]
Below is some cropped code from my problematic model.
The problem is the function load(). When used within the get() function, it works as it should. When used in the get_associated() function, it throws the following error:
Code:
Fatal error: Call to a member function on a non-object in /var/www/codeigniter/application/models/modelplus.php on line 83
Line 83 is the line "$this->db->where('id', $id);" inside the load() function.
My gut reaction is that the problem resides in the scope of $this, but why would the load() function work from within one of the model's function, but not another.
Any help is greatly appreciated.
Code:
<?php
class ModelPlus extends Model {
var $data_description;
var $table_name;
function ModelPlus() {
parent::Model();
$this->data_description = array();
$this->table_name = get_class($this);
}
function get_associated($model_name) {
if (!strcmp(get_class($this),$model_name))
return false;
// find the association table. association tables are titled in the format:
//
// [model1]_[model2]
//
// The model names are always in alphabetical order in the association table
// name.
$association_table = (strcmp(get_class($this), $model_name) < 0 ?
get_class($this) . '_' . $model_name :
$model_name . '_' . get_class($this));
$this->db->where(get_class($this) . '_id', $this->id);
$results = $this->db->get($association_table);
if ($results->num_rows()) {
$retval = array();
foreach ($results->result() as $r) {
eval('$tmp_id = $r->' . $model_name . '_id;');
eval('$tmp = new ' . $model_name . '();');
$tmp->load($tmp_id);
$retval[] = $tmp;
}
return $retval;
} else {
return null;
}
}
function load($id) {
if (!$id)
return false;
$this->db->where('id', $id);
$result = $this->db->get($this->table_name);
if ($result->num_rows()) {
$result = $result->result();
$result = $result[0];
foreach ($result as $key=>$value) {
$this->$key = $value;
}
} else
return false;
}
}
?>