omines / datatables-bundle

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

Access to entity fields in table builder #66

Closed graferowy closed 5 years ago

graferowy commented 5 years ago

Hi! What is the best way to acess all of the entity fields in my table builder? I've created seperate file implementing DataTableTypeInterface where I build my table I want to display using ORMAdapter. There problem is when using render field option. The $context variable holds only fields I have added to the table, the rest are set to null. Should I add a column with the field I want to access and set its visibility to false? Or is there a better way?

For example, here I would not have access to username field of my user entity.

class UsersTableType implements DataTableTypeInterface
{
    public function configure(DataTable $dataTable, array $options)
    {
        $dataTable
            ->add('id',
                TextColumn::class,
                array(
                    'label' => 'ID',
                    'render' => function ($value, $context) {
            return $context->getUserName(); }
                )
            )
    ->createAdapter(ORMAdapter::class, [
                'entity' => Coupon::class
    ]
    }
}
curry684 commented 5 years ago

If you create a custom query you will no longer be limited to the fields displayed by columns. The default query builder will select just the fields referenced by columns, once you switch to a custom query you're completely in control about what to do. And of course that entire object will then be passed as the $context.

graferowy commented 5 years ago

Thank you very much. Works perfectly.