result_array(), result_object() to accept a field name to use as key of returned array #429

Open
wants to merge 8 commits into
from

Conversation

Projects
None yet
8 participants

wyred commented Sep 12, 2011

http://codeigniter.com/forums/viewthread/193209/

http://codeigniter.uservoice.com/forums/40508-codeigniter-reactor/suggestions/1561893-pass-column-name-into-active-record-result-to-be

Example use:
In my users database table, I have:

id  - name - email
1 - Preston - preston@libria.com
3 - Seamus - seamus@libria.com
4 - Dupont - dupont@libria.com

In my app, I do:

$rs = $this->db->select('id, name')->get('users');
$users = $rs->result_array('id');

$users will be:

Array
(
    [1] => Array
        (
            [id] => 1
            [name] => Preston
        )

    [3] => Array
        (
            [id] => 3
            [name] => Seamus
        )

    [4] => Array
        (
            [id] => 4
            [name] => Dupont
        )
)

@wyred wyred commented on the diff Sep 12, 2011

system/database/DB_result.php
{
- $object = new $class_name();
-
- foreach ($row as $key => $value)
+ $row = $this->_fetch_object();
+ if (isset($row->$object_key)) $key_exists = TRUE;
+ $this->_data_seek(0);
+ }
+
+ if ($key_exists) {
@wyred

wyred Sep 12, 2011

Instead of evaluating if $key_exists at line 98, I chose to duplicate the code just so that $key_exists will only need to be evaluated once instead n times for n results.

Really good idea!

I also think this makes sense at first glance...

I love making the ID be the array key. This really simplifies the process. Good idea.

Contributor

joelcox commented Sep 13, 2011

Nice pull request. I often find myself looping over the result array to do just this. Don't forget to actually add this functionality to the docs, not just to the changelog.

wyred commented Sep 13, 2011

Thanks everyone!

@joelcox is it ok to proceed with user_guide changes yet?
Last I heard was this: http://twitter.com/#!/philsturgeon/status/108154732987686913

Contributor

joelcox commented Sep 13, 2011

The changelog is also included in the user guide and you did add your change there ;-). You can write the documentation now, just don't expect it to be merged directly and you may need to copy it to the new format later (or maybe an engineer reformats it for you.)

Thanks everyone!

@joelcox is it ok to proceed with user_guide changes yet?
Last I heard was this: http://twitter.com/#!/philsturgeon/status/108154732987686913

Reply to this email directly or view it on GitHub:
EllisLab#429 (comment)

Contributor

ftwbzhao commented Sep 14, 2011

really good

wyred commented Sep 15, 2011

If this request is being ignored because the code is messy, would it be better if we create a new function instead?

Something like: $query->result_array_key('id');

I'm not good at naming this. D:

Contributor

joelcox commented Sep 15, 2011

I actually like the original way where you pass the key name to the result/result_array method. Read this to see why your request isn't merged yet.

wyred commented Sep 15, 2011

Yes, certainly, I understand that very well. :)

Just wanted to suggest this alternative in case they actually find my solution messy.

My bad for sounding impatient

On Sep 15, 2011, at 6:06 PM, Joël Coxreply@reply.github.com wrote:

I actually like the original way where you pass the key name to the result/result_array method. Read this to see why your request isn't merged yet.

Reply to this email directly or view it on GitHub:
EllisLab#429 (comment)

the key is not working while cache is on ?

AmitMY commented Dec 2, 2016

+1
Great idea IMO. Any reason it was not merged in the last 5 years?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment