dbal icon indicating copy to clipboard operation
dbal copied to clipboard

Ability to disable normalization

Open mskocik opened this issue 1 year ago • 4 comments

  • Is your feature request related to a problem? Please describe.

Sometimes is useful to get raw data from DB. For example I am exporting some data into CSV file. In current state of things can't simply pass $result->fetchAll() into CSVResponse (from Contributte). I need to iterate the array and convert Row to array first. And if there are DateTimes, need to convert them as well. When exporting large datasets it's useless to convert to Row and to array again and stringify everything again.

  • Describe the solution you'd like.

It would be great if we can switch off data normalization. And even better if we could also get array<array> instead of array<Row>.

/** @var Row[] */
$denormalized = $result->raw()->fetchAll();

/** @var array<array> */
$arraysOfArray = $result->raw('array')->fetchAll();
  • Describe alternatives you've considered.

Alternative would be ability to provide custom Result class

mskocik avatar Jun 20 '23 09:06 mskocik

Result has setValueNormalization(enabled: false) that's not enough? Array of array is probably to difficult to do without much added value.

hrach avatar Jun 20 '23 16:06 hrach

I totally missed that method. 🤯🤦

Regarding array of array I was playing with it and Result could add new method (jus one) for that like this:

// Result.php
public function fetchAllAsArray(): array
{
    $data = [];
    while ($row = $this->adapter->fetch()) {
        $data[] = $this->normalize($row);
    }
    return $data;
}

mskocik avatar Jun 20 '23 16:06 mskocik

And what's your motivation here to have an array? Why is it better than row?

hrach avatar Jun 20 '23 19:06 hrach

As I wrote above: sometimes it's needed to work with arrays anyway, like using fputcsv where conversion to Row and then back to array is useless. Especially with large datasets.

But that would be only nice to have. Main point of this issue is solved by pointing me to setValueNormalization, so feel free to close the issue, if you don't consider array method useful. Workaround exists.

mskocik avatar Jun 20 '23 19:06 mskocik