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

[eluser]Unknown[/eluser]
I did a search for Models and their functionality and really just saw people complaining...

blah.

I understand the idea of the base Model being bland. However I am curious to know how I would accomplish something. Beyond using the database library I am interested in creating functions such as getLatest(); that I can use among several models such as articles, members, blogs etc. Would I achieve this by importing the database library into a functions library where I would define these functions. Then bring that library into each Model? If not please shed some light. I am catching on quickly but could use some help so I don't have to rack my brain to much.

Thanks
#2

[eluser]stuffradio[/eluser]
Hey Jenks.

I had to do some googling to find out what exactly Models did, and then I searched the CI user guide to see how to use them. You're in luck though because I will show you how to use it.

Say you wanted to get everything from a database. You have your controller like this:

Code:
<?php

class Information extends Controller {

  function Information()
  {
    parent::Controller();
  }

  function index()
  {
  }

}

That's the core of the controller. So lets write an example model:

Code:
<?php

class Latest extends Model {

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

}

So, I believe this would be the best name for the model because you're basically getting the latest info of different things. Lets make a new function in the Model class for getting News articles.

We're going to get all the latest articles. Put this underneath 'function Latest()'

Code:
function news()
  {
   $query = $this->db->get('news');
   return $query->result();
  }

In the Controller we need to load the model now.

Code:
function Information()
  {
    $this->load->model('latest');
  }

We can now pull all results from the model in the index page.

Code:
function index()
  {
   $display['info'] = $this->latest->news();
   $this->load->view('index', $display);
  }

We now need a view file to handle the results. Call it index.php and put it in your views folder.

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

$info is the variable containing the array of info from the database, we named it $news. Message is the field in the news table. That's where I got those things from.

It's very hard for me to see if I got everything right right now, so I will put it all together and look after I post.

information.php (Location: system/application/controllers/)

Code:
&lt;?php

class Information extends Controller {

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

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

}


latest.php (Location: system/application/models/)

Code:
&lt;?php

class Latest extends Model {

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

  function news()
  {
   $query = $this->db->get('news');
   return $query->result();
  }

}

index.php (Location: system/application/views/)
Code:
&lt;?php foreach($info as $news):?&gt;
<h1>Static Headline</h1>
<br />
&lt;?php echo $news->message; ?&gt;
&lt;?php endforeach; ?&gt;


I got a bit carried away with this bit of information, but I hope you can get some info from it Tongue
#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:
&lt;?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:
&lt;?php foreach($info as $news):?&gt;
<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!




Theme © iAndrew 2016 - Forum software by © MyBB