Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Result's fetchPairs() doesn't respect unset data in the original result #111

Open
smuuf opened this issue Oct 22, 2015 · 0 comments
Open

Comments

@smuuf
Copy link

smuuf commented Oct 22, 2015

The premise:

Table 'users'
+------+------+
|   id | name |
+------+------+
|    1 |    a |
|    2 |    b |
|    3 |    c |
|    4 |    d |
|    5 |    e |
+------+------+

Code:

// Fetch all rows from a table
$result = $this->db->users();

unset($result[3]);
$array = iterator_to_array($result);
$pairs = $result->fetchPairs('id', 'name'));

$pairs:

[
 1 => "a",
 2 => "b",
 3 => "c",
 4 => "d",
 5 => "e",
]

Instead of expected:

[
 1 => "a",
 2 => "b",
 4 => "d",
 5 => "e",
]

While $array really is:

[
 1 => NotORM_Row { ... },
 2 => NotORM_Row { ... },
 4 => NotORM_Row { ... },
 5 => NotORM_Row { ... },
]

I noticed NotORM_Result::fetchPairs() actually clones the result itself, modifies the query and then executes it again - losing/bypassing whatever changes were made to the original result object.

Am I wrong for considering this a buggy behaviour? The results might be truly unexpected and bugs coming out of this are really difficult to debug, since one lives under the impression that he operates on an already defined (and possibly modified) result.

Plus the fact that we already have the data fetched, so there's really no need for executing another query - again, or is there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant