Illegal offset type when using NTableSelection::fetchPairs()

Notice: This thread is very old.
WindBridges
Member | 10
+
0
-

I using php 5.2 version. I get this error when trying to convert NTableSelection to array using ->fetchPairs(“date”) where “date” field has DATE type, because in code below $row[$key] can not be an object while using as array key:

<?php
// libs/Nette/Database/Table/Selection.php @ line 872
$return[$row[$key]] = ($value !== '' ? $row[$value] : $row);
?>

Additionally, I have small feature request. It will be good if it will be able to fetch results as true array, not an array of objects as it done now.

Last edited by WindBridges (2012-03-10 16:54)

duke
Member | 650
+
0
-

Definitely seems like bug in Nette. I guess there should be:

$return[is_scalar($row[$key]) ? $row[$key] : (string) $row[$key]] = ($value !== '' ? $row[$value] : $row);

As for your request, it is usually convenient to have datetime values given as Nette\DateTime objects. After all you can always easily convert them into strings (if you echo them or concatenate them to strings, the conversion is automatic and in other cases you can use explicit conversion).

hrach
Member | 1834
+
0
-

Thank you for your report. I'll bugfix that.
However, what's your limitation with array of object? ActiveRow is pretty straightforward.

Last edited by hrach (2012-03-11 17:11)

WindBridges
Member | 10
+
0
-

I just thought it's unreasonable to have one object per row on large data selections. But now I understand that even if we will have such method to return true array, it will keep array of objects in memory anyway. So I was wrong ;)

hrach
Member | 1834
+
0
-

I've already sent the patch, just waiting for merge.
https://github.com/…tte/pull/574

duke
Member | 650
+
0
-

@hrach Are you sure the explicit (string) conversion is sufficient? What if someone will fetchPairs on column of type bool? If you always convert to string, you wont have TRUE and FALSE as keys in fetched array… That's why I suggested using is_scalar

hrach
Member | 1834
+
0
-

Yeah duke, I am sure. It's not well known but php converts everything to int if its possible.
So,

array("1" => true) === array(1 => true);
array(true => true) === array(1 => true);

Unfortunatelly, it's imposible to let “1” key to be string → almost without problem because echo $array[“1”] is again called as echo $array[1], however ther are some cases when you need to maintain string key: http://programujte.com/…p-a-xml-rpc/

hrach
Member | 1834
+
0
-

And sadly you were right. I didn't realize this relation:

array((string) false => 1) === array("" => 1) // of course is not possible to convert to int... :/

but it works for true… :) (should be enough :P)

Last edited by hrach (2012-03-11 20:23)

duke
Member | 650
+
0
-

This is exactly why I used is_scalar. And bool wouldn't be the only problem, floats wouldn't work as well, because $foo['1.5'] is not same as $foo[1.5] (well it would work, but in a bit WTF way). But I see on the fixed commit that your “should be enough” was meant as a joke. :-)

Last edited by duke (2012-03-11 21:53)

hrach
Member | 1834
+
0
-

@duke: no, only false would be affected. $foo[1.5] call would be also called as $foo['1.5'].

Last edited by hrach (2012-03-20 19:57)