Welcome Guest, Not a member yet? Register   Sign In
Views: how to do this correctly?
#11

[eluser]Randy Casburn[/eluser]
Ouch! Do folks really write a model method for each page? So much for reuse. I can see why some folks say CI "isn't really OO". I'm currently working a site with more than ~17 pages and counting - One (1) model class, two primary methods. There are only two methods because there are direct calls for the data or HttpRequest calls for the data. I handle JSON separately from PHP arrays.
#12

[eluser]xwero[/eluser]
You can write a basemodel with abstracted methods that are reusable and create a childmodel with a method for each page so you can do data manipulations, display specific queries and other things. If it's needed of course. I don't think it's possible to create an abstracted method for all purposes or you have to add the sql statement to the view files but that is not recommended.
#13

[eluser]Randy Casburn[/eluser]
Now were becoming academic ;-)
#14

[eluser]xwero[/eluser]
I think it's practical approach. If you have a better approach i would love to hear it.
#15

[eluser]Randy Casburn[/eluser]
I wasn't digging on you. No we're just back to this...
Quote:It would be interesting to see a front end design tool scouring through all the Model files correcting queries (teehee)
There are many ways to accomplish one goal. What makes one method any better or worse than another is determined by one's frame of reference.

===============

One class, one method:

Assuming the final URI segment is used to identify the page name from the database (routing used to establish this for instance):

From within your Controller some where:
Code:
/* i.e. http://mydomain.com/whatchuwant */
$this->_getPageData($this->uri->segment($this->uriSegments));

This is also a method of your controller:
Code:
/* requests page data for page named
* 'whatchuwant' from model
*/
function _getPageData($page){
  $this->load->model('myModel');
  $this->pageData = $this->myModel->getPageDataFromDB($page);
}

Then the model would look like this:
Code:
function getPageDataFromDB($page){
  $query = $this->db->getWhere('content','alias="'.$page.'"');
  $aPageMeta = $query->row_array();
  return $aPageMeta;
}

Finally, back to your controller, call your views thus...
Code:
$this->load->view('myview', ( $this->pageData)? $this->pageData : $this->page404() );

===========
That's it. You can call any number of pages out of your DB with only this code and nothing else.

There is an unmistakable simplicity in this. As I said above, your frame of reference will be different than mine. Please consider that before you decompose every line of this demonstrative code.




Theme © iAndrew 2016 - Forum software by © MyBB