[eluser]Jay Turley[/eluser]
As is common with many web applications, I have to generate a list of items from the database. Now, the easy way would be to use a simple query (with or without active record):
Code:
class Foolist_model extends Model {
function Foolist_model() {
parent::Model();
}
function getAllFoos($user_id) {
$this->db->where('creator_id', $user_id);
return $this->db->get(foos);
}
}
However, I already have a Foo model:
Code:
class Foo_model extends Model {
var $id;
var $bar;
var $creator_id;
function Foo_model() {
parent::Model();
$this->id = '';
$this->bar = '';
$this->creator_id = '';
}
function getId() {
return $this->id;
}
function getBar() {
return $this->bar;
}
function setBar($bar) {
$this->bar = $bar;
}
function getCreatorId() {
return $this->creator_id;
}
function setCreatorId($creator_id) {
$this->creator_id = $creator_id;
}
function load($foo_id) {
if ($foo_id > 0) {
$this->db->where('id', $foo_id);
$query = $this->db->get('foos');
if ($query->num_rows() > 0) {
$row = $query->row();
$this->id = $row->id;
$this->bar = $row->bar;
$this->creator_id = $row->creator_id;
return true;
} else {
return false;
}
} else {
return false;
}
}
}
And I'm thinking the "right" way to do this is to generate an array of Foo_models, and then pull the information out of them. This is obviously the hard way, and it leads me to some questions which I am hoping some experienced users can help me figure out:
(1) Can I load a Foo_model() inside of the Foolist_model()? Can we load models inside models?
(2) What kind of memory impact am I looking at? How about speed? I am going to have to post-process the query in either case to dump it into JSON format, so the apparent simplicity of the easy way may not be that much of a time saver.
I would appreciate any insight into this issue. Thanks a ton!