omines / datatables-bundle

DataTables bundle for Symfony
https://omines.github.io/datatables-bundle/
MIT License
258 stars 115 forks source link

Error on Omines Datatable when builden a Query in CreateAdapter #80

Closed mschrading closed 5 years ago

mschrading commented 5 years ago

On building a data collection in omines datatable in symfony 4.2 and using QueryBuilder I have this

function (QueryBuilder $builder) { $builder ->distinct() ->select('m, b, v, m.lfd') ->from(Meldung::class, 'm') ->leftJoin('m.lastUser', 'b') ->leftJoin('m.process', 'v') ->leftJoin('m.messageType', 't'); } I got this error message from symfony:

Cannot read property "id" from an array. Maybe you intended to write the property path as "[id]" instead.

This error message comes when I add m.lfd (or some other specific field) in the select list

I don't know how to handle this.

MaximePinot commented 5 years ago

Hi,

You need to set the hydrate option to Query::HYDRATE_ARRAY:

->createAdapter(ORMAdapter::class, [
    'hydrate' => \Doctrine\ORM\Query::HYDRATE_ARRAY,
    'entity' => Meldung::class,
    'query' => function (QueryBuilder $builder) {
        $builder
            ->distinct()
            ->select('m, b, v, m.lfd')
            ->from(Meldung::class, 'm')
            ->leftJoin('m.lastUser', 'b')
            ->leftJoin('m.process', 'v')
            ->leftJoin('m.messageType', 't');
    }
]
shades684 commented 5 years ago

I think removing the distinct() and not selecting the m.lfd from your query would do the trick. Why would you select it seperatly anyway, it's part of the entity you were selecting in the first place and since you are using the ORMAdapter you're actually expect it to return one of your objects: Meldung

mschrading commented 5 years ago

Thank you friends I'll try that out

mschrading commented 5 years ago

I've checked that out and I get only one result set array[0]! So how can I iterate through all results? Thank you for any help

mschrading commented 5 years ago

By setting 'hydrate' => \Doctrine\ORM\Query::HYDRATE_ARRAY, I'll get an array with one set of data which will be iterated trough all result sets. On using goupedBy I'll get all resuzkltssets in an array which I have to loop through but how to use this in a Omines datatable? Thank you for any hint

shades684 commented 5 years ago

https://github.com/omines/datatables-bundle/blob/master/tests/Fixtures/AppBundle/DataTable/Type/Grouped2TableType.php