• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manipulating data returned by Model

#1
[eluser]Unknown[/eluser]
Hi guys,

This is a newbie question... I got the following model function returning all the blog posts in the database matching the criterias :

Code:
function get_posts($id_website,$limit){
        $this->db->select('*');
        $this->db->from('blog_post');
        $this->db->where('id_website',$id_website);
        $this->db->orderby('post_date','desc');
        $this->db->limit($limit);
        $query = $this->db->get();
        return $query;
    }

Since I'm going to use that function over many controllers, I'd like to do some data manipulation and cleansing before returning the data to the controller. For example, i'd like to tranform fields that returns 0 or 1 to yes or no, do calculation on implied values, or be able to create a new property to the returned object... What would be the best practice in order to achieve this ?

#2
[eluser]bretticus[/eluser]
If your data should be uniform across controllers, then the best way is to loop your $query:

Code:
foreach ($query->result() as $row) {
//build new array or object per logic statements...
}

...and build a new array or object in the model method before returning to the controller.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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