Codeigniter Database Results - Objects or Arrays?

I have been using Codeigniter for a long time, and I often wondered what / whether to use Codeigniter

$query->result() or $query->result_array()

Personally, I tended to stick with arrays, since it is easier for me to manipulate the array and click elements, etc. The only thing that annoys me a bit is just to write statements in the view, for example.

$row->title much cleaner and easier to read than $row['title']

but

$row->{0} more than $row[0] , on the other hand, not!

I'm still confused as to which way to go, if anyone can tell me more benefits of using objects over arrays, I would be delighted!

+4
source share
1 answer

I prefer objects, but there is an advantage in using arrays that can reduce clutter in situations that require some checking or operation for all (or some) columns:

 foreach ($row as $column => $value) { if ($value == null) { // oh no! null! } } // check these columns and make sure they're non-negative $non_negative = array('amount_paid', 'income'); foreach ($non_negative as $column) { if ($row[$column] < 0) { // less than zero? what a bum. } } 
+2
source

Source: https://habr.com/ru/post/1482382/


All Articles