ext-ds icon indicating copy to clipboard operation
ext-ds copied to clipboard

Ds\Map do not support mysqli result fetch_object

Open breath-co2 opened this issue 8 years ago • 5 comments

<?php
$mysql = new mysqli('localhost', 'root', '***', 'test');
$rs = $mysql->query('select * from `mydb` limit 10');
while ($row = $rs->fetch_object('\\Ds\\Map'))
{
    print_r($row);
}

output:

PHP Recoverable fatal error:  Object of class Ds\Map could not be converted to boolean in /test.php on line 4
<?php
$mysql = new mysqli('localhost', 'root', '***', 'test');
$rs = $mysql->query('select * from `mydb` limit 10');
while (null !== ($row = $rs->fetch_object('\\Ds\\Map')))
{
    print_r($row);
    var_dump($row->id);
    var_dump(isset($row['id']));
}

output:

Ds\Map Object
(
)
string(1) "1"
bool(false)
Ds\Map Object
(
)
string(1) "2"
bool(false)

breath-co2 avatar Mar 22 '17 05:03 breath-co2

I believe this is because Ds\Map doesn't use the object's properties table to store the data. I don't see a way to support this, sorry. Could just fetch as array and create map using that data.

rtheunissen avatar Mar 22 '17 22:03 rtheunissen

@rtheunissen thank you.

If it implement magic method __set($key, $value) can support it, like this php code:

class Ds\Map
{
    function __set($key, $value)
    {
        $this[$key] = $value;
    }
}

or Ds\Map implement the ArrayAccess interface. It's my imagine.

breath-co2 avatar Mar 23 '17 02:03 breath-co2

I'm not sure if array access would solve this. What you could do is create a class like ResultMap that creates an instance of Ds\Map and gets/sets on that instance using its own __get and __set. That way you can fetch into ResultMap and expose a get() method to return the internal map.

Might be faster to just fetch an array and instantiate a new map with it, will need to benchmark that yourself to see what works best for you.

rtheunissen avatar Jul 14 '17 23:07 rtheunissen

This might also be related (and fixed) to #62

rvanvelzen avatar Aug 03 '17 06:08 rvanvelzen

#62 fixes the boolean cast issue but we still can't fetch into any of the structures.

rtheunissen avatar Aug 07 '17 23:08 rtheunissen