Welcome Guest, Not a member yet? Register   Sign In
advice about models
#1

[eluser]Unknown[/eluser]
I'm still try to understand how best to use CI's models and I'm looking for some advice:

Lets say I have a blog posts and each of those has comments.

I want to be able to get the last 10 blog posts which I would understand I could do in a model like this (from the docs)

Code:
class Blogmodel extends Model {
    var $title   = '';
    var $content = '';
    var $date    = '';
    var $id      = 0;

    function Blogmodel()
    {
        parent::Model();
    }
    
    function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }
}

What i would like to do is be able to use the Blogmodel as and object to do something like
Code:
foreach ($last_ten_blogs as $blog)
{
  $top_comments = $blog->get_top_comments();
}

and to use the "models" as stand alone objects. Does that work? Am I approaching it the right way? I was able to accomplish this uses "libraries" and I would just do this:
Code:
$this-load->library('Myfoo');
$newfoo = new Myfoo();
$newfoo->do_magic();

But I wasn't sure if that was really the right way to approach this - or if I could/should use models instead?




Theme © iAndrew 2016 - Forum software by © MyBB