Welcome Guest, Not a member yet? Register   Sign In
Extending DB_Result
#1

[eluser]Unknown[/eluser]
Hi, my first post after more than 2 years of using CodeIgniter...

I have a very needed db function that I'm using in other projects that are not based on CodeIgniter.

When using a function to return rows from a query (same like $res->result_array()), I give an extra parameter to be used as index field for the array keys.

For example, if I have countries table, with code & name, instead of (example syntax):
countries[0] = array(code: US, name: united states)
countries[1] = array(code: TH, name: thailand)

I want to receive (by calling the function $res->result_array('code')):
countries['US'] = array(code: US, name: united states)
countries['TH'] = array(code: TH, name: thailand)

To add this functionality, I need to override DB_Result->result_array()

I tried to find simple ways to do it, but couldn't. I read http://codeigniter.com/wiki/Extending_Database_Drivers/ but it didn't help.

If anyone can help with a solution, I'll be very happy. I believe this functionality should be part of CodeIgniter core code, since it's very useful and doing it in the core saves a lot of extra processing, but I'll settle for a solution to extend DB_Result Smile
#2

[eluser]Dready[/eluser]
Hello,

it's painful to extend core database drivers in CI, perhaps the simplest way to do what you need is to create an helper function, i.e. indexed_result($res,'code') , and then call it instead of $res->result_array() .




Theme © iAndrew 2016 - Forum software by © MyBB