omines / datatables-bundle

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

Seperate query from table builder #67

Closed graferowy closed 5 years ago

graferowy commented 5 years ago

Hello!

What would be the best way to seperate query code from the datatable builder code itself? My queries can get long and I'd like to keep my datatable type class as clean as possible.

class MyTableType implements DataTableTypeInterface
{
    public function configure(DataTable $dataTable, array $options)
    {
        $request = $options['request'];

        $dataTable
            ->add('id',
                NumberColumn::class,
                array(
                    'label' => 'ID',
                    'globalSearchable' => false
                )
            )

... Some more columns ...

->createAdapter(ORMAdapter::class,
                array(
                    'entity' => MyEntity::class,
                    'query' => function (QueryBuilder $builder) use ($request)
                    { //Long query I'd prefer to get from somewhere else }
                )
            )
        ;
    }
}

Do I need to create a class that implements QueryBuilderProcessorInterface for each entity I'd build my tables for?

graferowy commented 5 years ago

Ok, I don't know if this is the best solution, but what I've done is:

class MyTableType implements DataTableTypeInterface
{
    public function configure(DataTable $dataTable, array $options)
    {
        $request = $options['request'];
        $em = $options['entityManager'];

        $dataTable
            ->add('id',
                NumberColumn::class,
                array(
                    'label' => 'ID',
                    'globalSearchable' => false
                )
            )

... Some more columns ...

->createAdapter(ORMAdapter::class,
                array(
                    'entity' => MyEntity::class,
                    'query' => function (QueryBuilder $builder) use ($request, $em)
                    { $em->getRepository(MyClass::class)->find($request, $builder) }
                )
            )
        ;
    }
}

Inside my repository I return passed QueryBuilder variable and that's all.

curry684 commented 5 years ago

What would be the best way to seperate query code from the datatable builder code itself?

Long story short - you shouldn't, and that's why there's no facility to easily integrate entity repositories with the data tables types. Because, and this is fundamental, repositories serve not just a different but entirely conflicting purpose from what a data table is doing.

Repositories are meant to organize reusable and parametrized queries. They may, at will, expose variables, parameters and options affecting their behavior, in a query-specific way.

Data Tables are centered around one single query, that needs to get modified dynamically along the way to provide filtering, ordering, and pagination on the fly.

As such, it makes most sense to plug a DQL query directly in the DataTableType, as it lays the foundation that way for being modified.