Welcome Guest, Not a member yet? Register   Sign In
Lists of Items: the fast way and the slow way? What is the right way?
#1

[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!
#2

[eluser]wiredesignz[/eluser]
I can help with (1) You can force a Model to load inside another Model, but its absolutely not recommended.
Try to re-organise your queries using joins or something similar. Even extending one Model from another is a better choice.
#3

[eluser]Jay Turley[/eluser]
Thanks for that bit of knowledge.

I'm not worried about writing complex joins; that's old hat. My question is more about doing the Right Thing ™.

I mean, if I were building some sort of massive application with an application server that could persist objects in memory, then I would build a list of of little Foo()s and hold them in memory until I could do a massive batch save, thus cutting out the db layer.

But since CI does not have a persistent app layer (not that I mind, you get me?) I am wondering if it is worth the trouble to try to resuse the Foo() objects or if I should just do the easy way.

The problem is it's TOO DAMN EASY!!! (eyes code suspiciously)




Theme © iAndrew 2016 - Forum software by © MyBB