omines / datatables-bundle

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

Problem with custom query #169

Closed polo195pl closed 3 years ago

polo195pl commented 3 years ago

Hi everyone :) I use omines/datables-bundle with Symfony 4.4 and Doctrine 2.4 and PHP 7.3.12

I have two entietes: User with fields: id, firstname, lastname and logs and entity Log with fields: id, user, time

So i try builid table:

$table = $this->datatableFactory->create([])
   ->add('id', TextColumn::class, ['label' => '#', 'className' => 'bold', 'searchable' => true])
   ->add('lastname', TextColumn::class, ['label' => $translator->trans('Customer name'), 'className' => 'bold', 'searchable' => true])
   ->add('logStart', DateTimeColumn::class, ['label' => $translator->trans('Time'), 'className' => 'bold', 'searchable' => false])
   ->createAdapter(ORMAdapter::class, [
                'hydrate' => \Doctrine\ORM\Query::HYDRATE_ARRAY,
                'entity' => Log::class,
                'query' => function (QueryBuilder $queryBuilder) {
                    $queryBuilder            
                        ->select('l, u')
                        ->from(Log::class, 'l')
                        ->leftJoin('l.user', 'u');                                                                   
                }
            ]); 

But when i run this code i have only data from Log entity without User I would be very grateful for your help :) Thx all 👍

polo195pl commented 3 years ago

Problem solved in many to one relation we must use 'field' option for example:

        $table = $dataTableFactory->create()
            ->add('firstName', TextColumn::class, ['label' => 'Firstname', 'field' => 'user.firstname'])
            ->add('logStart', DateTimeColumn::class, ['label' => 'Log start'])
            ->createAdapter(ORMAdapter::class, [
                'entity' => Log::class,
            ])
            ->handleRequest($request);