![]() |
Where to format data query results - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Where to format data query results (/showthread.php?tid=54075) |
Where to format data query results - El Forum - 08-21-2012 [eluser]gfraser99[/eluser] I am wondering what the best practice is for formatting data query results, as in should this be done in the model, controller or view. Here is the exact example I am dealing with. I want to populate a select list which takes an array with 1 key and 1 value to populate. Code: Array My model query result returns all records and all columns: Code: Array The array I need to pass to my view does not match the format my model returns the data in. 1. Should I build another model method to return only the data I need? 2. Should I format the returned data to the array format I require to populate my select list in my view within the controller? 3. Should the logic be built in the view? Where to format data query results - El Forum - 08-21-2012 [eluser]TWP Marketing[/eluser] The model is intended to access your database and then massage the returned data into a format needed by the controller, which in turn passed it to the view in finished or almost finished form. The controller may need to manipulate data received from the model, based on other values, such as configuration, login status, etc., but do most of the formatting in the model. That way, if you someday change the db, you only need to change your model and not the controller or views. About the only data manipulation done in the view is to loop an array of values to be echo'd to display. Your option 1 is the preferred MVC way of handling data. Where to format data query results - El Forum - 08-21-2012 [eluser]gfraser99[/eluser] Yes, the way I have implemented it is to create a common method that all models can access which does this: Code: /** Where to format data query results - El Forum - 08-21-2012 [eluser]TWP Marketing[/eluser] So you have a generic method to read a table. Now you can call that method from another model method (Same model file) and process the result(s) from the generic method to format it as needed by the controller. All the work is done in the model. [EDIT] If you want your model method(s) to be accessible from other models, you might look into a library instead. The User Guide is your friend<G>... Where to format data query results - El Forum - 08-21-2012 [eluser]Aken[/eluser] [quote author="TWP Marketing" date="1345573536"]If you want your model method(s) to be accessible from other models, you might look into a library instead.[/quote] Be easier to just load models inside models. Where to format data query results - El Forum - 08-22-2012 [eluser]Unknown[/eluser] Code: /** Where to format data query results - El Forum - 08-22-2012 [eluser]gfraser99[/eluser] Actually..what I did to was make MY_Model extend CI_Model and store it in the application/core folder. Then all my other models extend off MY_Model, such as User_Model. This enables me to create a parent method in My_Model like: primary_table is set as a property in the child model class. Code: /** Code: $this->load->model('model_name'); Where to format data query results - El Forum - 09-17-2012 [eluser]Alejus[/eluser] The last code in the post have some bugs, this works for me: In the controller: Code: $data['lista_tipos'] = $this->global_model->get_Sec('tipos_id','nombretipos','tipos'); //$id, $name, $from In the model Code: function get_Sec($id, $name, $from){ In the view: Code: $it = $isi['tipos_id']; // sent by the form Alejus |