Illegal offset type when using NTableSelection::fetchPairs()
- WindBridges
- Member | 10
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
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).
- WindBridges
- Member | 10
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 | 1838
I've already sent the patch, just waiting for merge.
https://github.com/…tte/pull/574
- hrach
- Member | 1838
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/
- duke
- Member | 650
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)