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

Hi guys,

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

function get_posts($id_website,$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 ?

If your data should be uniform across controllers, then the best way is to loop your $query:

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-2021 MyBB Group.