CodeIgniter Forums
What format should rows of data be in when passed from a Model back to a Controller? - 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: What format should rows of data be in when passed from a Model back to a Controller? (/showthread.php?tid=35899)



What format should rows of data be in when passed from a Model back to a Controller? - El Forum - 11-15-2010

[eluser]callumd[/eluser]
I have plenty of experience with PHP/MySQL but very limited experience with frameworks and CodeIgniter.

I'm just trying to make sure I get my "best practises" sorted out nice and early.

I have a question:

Is it bad practise to pass a database object back to a controller from a model?

Eg, if I hit a database inside a model to get some data, should I convert it in to an array and pass back the array?

Or should the controller convert the database object in to an array?

Or should it never happen, and the database object should be passed "as is" on to the view which then loops over it displaying each row?

What is the best practise here?

Thanks.


What format should rows of data be in when passed from a Model back to a Controller? - El Forum - 11-15-2010

[eluser]dudeami0[/eluser]
The choice between and ArrayObject and Array is made in the model usually, choosing either $query->result() for an ArrayObject or $query->result_array() for an Array. Then just simply return either to your controller and it'll work like a charm.

Basic rule of thumb for what happens where is:

Controller handles incoming information and calls models and views
Model processes the input for output
View outputs the data, generally a UI


What format should rows of data be in when passed from a Model back to a Controller? - El Forum - 11-15-2010

[eluser]callumd[/eluser]
Thanks dudeami0. I'm not familiar with ArrayObject, I might look in to it.

I wasn't familiar with result_array(), this interests me as well.