nextras / dbal

Database Abstraction Layer – concise secure API to construct queries & fetch data
https://nextras.org/dbal
MIT License
79 stars 28 forks source link

Ability to disable normalization #214

Open mskocik opened 1 year ago

mskocik commented 1 year ago

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.

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();

Alternative would be ability to provide custom Result class

hrach commented 1 year ago

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

mskocik commented 1 year ago

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;
}
hrach commented 1 year ago

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

mskocik commented 1 year ago

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.