How to strtolower all query result keys |
[eluser]skunkbad[/eluser]
I've been working on a project with many models, and because I am not in complete control of the database, many table fields have uppercase characters. I have been using strtolower on the keys, but would like to implement a solution that extends DB_result.php. I'm tired, so I'm just looking for a little help if anyone has the time. I'd like to ensure that I don't break anything by extending the class, because everything else needs to work the way it should. Any help is appreciated.
[eluser]d1a8lo24[/eluser]
Just use the lower() in MySQL when you're pulling in the data. see the following link to string function on MySQL function. http://dev.mysql.com/doc/refman/5.0/en/s...tion_lower
[eluser]skunkbad[/eluser]
[quote author="d1a8lo24" date="1310847204"]Just use the lower() in MySQL when you're pulling in the data. see the following link to string function on MySQL function. http://dev.mysql.com/doc/refman/5.0/en/s...tion_lower[/quote] That won't work because I don't need to lowercase the field values, I need to lowercase the field names (keys). For instance: Code: $row = array( What I need is for `Field-Name` to be 'field-name', and value should remain Value. The project I'm working on has about 40 tables, and all of the field names are Upper-Cased, which is less convenient for me.
[eluser]BradEstey[/eluser]
Have you tried array_change_key_case()? And if you have to do it from a SQL query... you can always define your own aliases in the query: Code: SELECT
[eluser]skunkbad[/eluser]
[quote author="Bradillac" date="1310857666"]Have you tried array_change_key_case()? And if you have to do it from a SQL query... you can always define your own aliases in the query: Code: SELECT Yes, array_change_key_case() or strtolower() will both change the case, and it's not that I don't know how to change the case, it's that I want to extend the DB class(es) to make this happen automatically for every query ... forever.
[eluser]BradEstey[/eluser]
I haven't had any luck extending the Database Driver.. but if you want to give it a shot, you could try reading this: http://codeigniter.com/wiki/Extending_Database_Drivers Otherwise, the quick & dirty fix is to open system/database/DB_result.php, find the "result_array()" function and change it to: Code: function result_array($lowercase = FALSE) And then find the function row_array() and change it to: Code: function row_array($n = 0, $lowercase = FALSE) So now if you call: $query->result_array(TRUE); then it will return a multi-row database query with all of the keys set to lowercase. If you call $query->row_array(0, TRUE); then it will return the first row of a query with the keys lowercased. The only disadvantage of this is that since you're editing the system files, every time you update Codeigniter you'll have to put these hacks back in.
[eluser]jmadsen[/eluser]
I played around with this a bit, and came to same conclusion as Bradillac... one idea i was playing with was to use the custom_result_object() and pass in your own class - but that didn't seem to get me any further than a different object to foreach over and reset everything. Just mention it in case it gives you any ideas
[eluser]skunkbad[/eluser]
Because I'm accountable for my actions, I can't knowingly modify system files, knowing that somebody else may work on the site later and not be able to figure out what happened when they upgrade CI. I've successfully extended the DB classes before, so I'll look into it tonight and post if I come up with a solution. I've got to drive about 1300 miles in the next 2 days, so I need to get to bed early, but I'm hoping to get this all figured out. Thanks for your help.
[eluser]skunkbad[/eluser]
I figured out how to extend DB_Result. Can't post all three files, so I'll show you them in three posts. First MY_Loader: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
[eluser]skunkbad[/eluser]
Second, MY_DB_mysqli_driver.php Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Welcome Guest, Not a member yet? Register Sign In |