• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
advice about models

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)

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

    function Blogmodel()
    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
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:
$newfoo = new Myfoo();

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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.