vrana/notorm

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

Opened this issue · 0 comments

smuuf commented

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?