omines / datatables-bundle

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

Use variables from Controller in Type Class #103

Closed manseuk closed 4 years ago

manseuk commented 4 years ago

I have the following controller function

$invoice = $this->entityManager->getRepository(Invoice::class)->find(1);
$itemSummaryTable = $this->createDataTableFromType(ItemSummaryTable::class)
            ->handleRequest($request);

And then i have class ItemSummaryTable like this

public function configure(DataTable $dataTable, array $options)
{
    $dataTable->->add('reference', TextColumn::class, ['label' => 'generic.reference', 'field' => 'item.reference1'])
    // more columns
    ->createAdapter(
            ORMAdapter::class,
            [
                'entity' => InvoiceSummary::class,
                'criteria' => [
                    function (QueryBuilder $qb) use ($invoice) {

How can i pass the $invoice from the controller to my ItemSumaryTable class ?

manseuk commented 4 years ago

Just discovered the 2nd and 3rd Parameters of createDataTableFromType - is this the correct way to pass in variables ?

shades684 commented 4 years ago

It's one of the ways to do it, without having more information it's hard to say if this is THE correct way.

Basically the same rules apply as te form component in Symfony.