omines / datatables-bundle

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

Depth > 2, multiple relation support with orderable/search functionalities #100

Closed grosssmutttr closed 4 years ago

grosssmutttr commented 4 years ago

Hi there,

I tried to get this working:

$table = $this->createDataTable()
                ->add('firstname', TextColumn::class, ['label' => 'Vorname', 'orderable'=> true, 'field' => 'personalData.firstname'])
                ->add('lastname', TextColumn::class, ['label' => 'Nachname', 'orderable'=> true, 'field' => 'personalData.lastname'])
                ->add('email', TextColumn::class, ['label' => 'Nachname', 'orderable'=> true, 'field' => 'personalData.lastname'])
                ->add('phone', TextColumn::class, ['label' => 'Telefon', 'orderable'=> true, 'field' => 'personalData.phone'])
                ->add('mobilePhone', TextColumn::class, ['label' => 'Mobil', 'orderable'=> true, 'field' => 'personalData.mobilePhone'])
                ->add('gender', TextColumn::class, ['label' => 'Gender', 'orderable'=> true, 'field' => 'personalData.gender.abbreviation'])
                ->add('lastModified', DateTimeColumn::class, ['label' => 'letzte Änderung', 'orderable'=> true, 'format' => 'd.m.Y H:i:s'])
                ->add('dateCreated', DateTimeColumn::class, ['label' => 'Datum Erstellung', 'orderable'=> true, 'format' => 'd.m.Y H:i:s'])
                ->add('actions', TextColumn::class, ['label' => 'Aktionen', 'orderable'=> false, 'render' => function($value, $context) {
                        $user_id = $context->getId();
                        return sprintf('<button type="button" data-user-id="%s" class="btn btn-primary edit-user-btn"><i class="fas fa-pencil-alt fa-fw fa-fw"></i></button><button type="button" data-user-id="%s" class="btn btn-danger del-user-btn margin-left-10"><i class="fas fa-trash fa-fw fa-fw"></i></button>', $user_id, $user_id);
                    }])
                ->createAdapter(ORMAdapter::class, [
            'entity' => User::class,
        ]);

This results in the following error: Catchable Fatal Error: Object of class App\Entity\Gender could not be converted to string

So as workaround I could add the __toString method to the entity but unfortunately it does not solve the problem so that this column is also orderable.

Then I tried this approach:

$table = $this->createDataTable()
                ->add('firstname', TextColumn::class, ['label' => 'Vorname', 'orderable'=> true, 'field' => 'personalData.firstname'])
                ->add('lastname', TextColumn::class, ['label' => 'Nachname', 'orderable'=> true, 'field' => 'personalData.lastname'])
                ->add('email', TextColumn::class, ['label' => 'Nachname', 'orderable'=> true, 'field' => 'personalData.lastname'])
                ->add('phone', TextColumn::class, ['label' => 'Telefon', 'orderable'=> true, 'field' => 'personalData.phone'])
                ->add('mobilePhone', TextColumn::class, ['label' => 'Mobil', 'orderable'=> true, 'field' => 'personalData.mobilePhone'])
                ->add('gender', TextColumn::class, ['label' => 'Geschlecht', 'orderable'=> true, 'render' => function($value ,$context) {
                        $user_id = $context->getId();

                        $user = $this->getDoctrine()
                                ->getRepository(User::class)
                                ->findOneBy(['id' => $user_id]);

                        return $user->getPersonalData()->getGender()->getAbbreviation();
                    }])
                ->add('lastModified', DateTimeColumn::class, ['label' => 'letzte Änderung', 'orderable'=> true, 'format' => 'd.m.Y H:i:s'])
                ->add('dateCreated', DateTimeColumn::class, ['label' => 'Datum Erstellung', 'orderable'=> true, 'format' => 'd.m.Y H:i:s'])
                ->add('actions', TextColumn::class, ['label' => 'Aktionen', 'orderable'=> false, 'render' => function($value, $context) {
                        $user_id = $context->getId();
                        return sprintf('<button type="button" data-user-id="%s" class="btn btn-primary edit-user-btn"><i class="fas fa-pencil-alt fa-fw fa-fw"></i></button><button type="button" data-user-id="%s" class="btn btn-danger del-user-btn margin-left-10"><i class="fas fa-trash fa-fw fa-fw"></i></button>', $user_id, $user_id);
                    }])
                ->createAdapter(ORMAdapter::class, [
            'entity' => User::class,
        ]);

Now I can see the table loading but if I try to order the table I'm getting the following error message: octrine\ORM\Query\QueryException: [Syntax Error] line 0, col -1: Error: Expected Doctrine\ORM\Query\Lexer::T_IDENTIFIER, got end of string.

at vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:54 at Doctrine\ORM\Query\QueryException::syntaxError('line 0, col -1: Error: Expected Doctrine\ORM\Query\Lexer::T_IDENTIFIER, got end of string.', object(QueryException)) (vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:457) at Doctrine\ORM\Query\Parser->syntaxError('Doctrine\ORM\Query\Lexer::T_IDENTIFIER') (vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:325) at Doctrine\ORM\Query\Parser->match(102) (vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:1022) at Doctrine\ORM\Query\Parser->ResultVariable() (vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:1521) at Doctrine\ORM\Query\Parser->OrderByItem() (vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:1408) at Doctrine\ORM\Query\Parser->OrderByClause() (vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:884) at Doctrine\ORM\Query\Parser->SelectStatement() (vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:848) at Doctrine\ORM\Query\Parser->QueryLanguage() (vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:261) at Doctrine\ORM\Query\Parser->getAST() (vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:360) at Doctrine\ORM\Query\Parser->parse() (vendor/doctrine/orm/lib/Doctrine/ORM/Query.php:286) at Doctrine\ORM\Query->_parse() (vendor/doctrine/orm/lib/Doctrine/ORM/Query.php:234) at Doctrine\ORM\Query->getResultSetMapping() (vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:902) at Doctrine\ORM\AbstractQuery->iterate(array(), 1) (vendor/doctrine/orm/lib/Doctrine/ORM/Query.php:689) at Doctrine\ORM\Query->iterate(array(), 1) (vendor/omines/datatables-bundle/src/Adapter/Doctrine/ORMAdapter.php:205) at Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter->getResults(object(Query)) (vendor/omines/datatables-bundle/src/Adapter/AbstractAdapter.php:51) at Omines\DataTablesBundle\Adapter\AbstractAdapter->getData(object(DataTableState)) (vendor/omines/datatables-bundle/src/DataTable.php:372) at Omines\DataTablesBundle\DataTable->getResultSet() (vendor/omines/datatables-bundle/src/DataTable.php:331) at Omines\DataTablesBundle\DataTable->getResponse() (src/Controller/UsersController.php:100) at App\Controller\UsersController->getUsers(object(Request)) (vendor/symfony/http-kernel/HttpKernel.php:151) at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1) (vendor/symfony/http-kernel/HttpKernel.php:68) at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true) (vendor/symfony/http-kernel/Kernel.php:198) at Symfony\Component\HttpKernel\Kernel->handle(object(Request)) (public/index.php:24)

So I have an Entity "User" and this has a relation to the entity "personalData" - in addition "personalData" has a relation to "gender" --> I want to have the gender data displayed in the datatables and keep the order and search functionalities working

Is there any workaround or is this a bug?