Welcome Guest, Not a member yet? Register   Sign In
Questions about Models
#3

[eluser]Unknown[/eluser]
Well I didn't think that worked like I wanted it to although I think maybe it will now that I wrote out some code. I am a newbie but have been buried in PHP, Python, C++ and soon to be Java books. I've decided plunging into writing some code is the best way to finally get on my feet. This could be COMPLETELY wrong or maybe on track somewhat I'm not sure.

Model
Code:
abstract class Latest extends Model {
    protected $tableSource

    function Latest()
    {
      parent::Model();
    }

    protected function setSource($tableSource){
        $this->tableSource=$tableSource;
    }
    
    // abstract db query method
    protected function getLatest($this->tableSource)
    {
        $this->load->database();

        private $table = $this->tableSource;        
        $query = $this->db->query('SELECT * FROM $table DESC LIMIT 5')
        
        return $query;
    }
}


class Blog extends Latest{
    
    function Blog()
    {
      parent::latest
    }

    function index()
        {
        $this->setSource('blog');
    $this->getLatest();
        }

}

Controller
Code:
<?php

class Latest extends Controller {

  function Latest()
  {
    parent::Controller();
    $this->load->model('latest');
  }

  function index()
  {
   // TO BE CODED LATER... Error thrown for now
   echo 'ERROR: URI must reflect table to query ex. latest/blog';
  }

  function Blog()
  {
      $display['info'] = $this->latest->blog();
      $this->load->view('blog', $display);
  }
}

View
Code:
<?php foreach($info as $news):?>
<h1>Static Headline</h1>
<br />
&lt;?php echo $news->message; ?&gt;
&lt;?php endforeach; ?&gt;

It's late so I know even if I am on track I missed something important but I'd love to know if I at least displayed any application of knowledge or if I missed the buck completely. Ideally what I would like is the abstract classes at the top to exist separately from the classes that extend them. So that way I can have a Model that pulls the Latest blog info using those extended classes, but also contain other such extended classes. I just need to know how would I include (actual include maybe?) those abstract classes which extend Model within these seperate models. I guess my idea is to keep my code dry by having these abstract functions I can build upon.

After reading your code though maybe it does this well enough already. Maybe I just need some sleep!


Messages In This Thread
Questions about Models - by El Forum - 08-06-2008, 12:13 PM
Questions about Models - by El Forum - 08-06-2008, 06:07 PM
Questions about Models - by El Forum - 08-10-2008, 01:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB