APY / APYDataGridBundle

Symfony Datagrid Bundle
MIT License
492 stars 343 forks source link

Calculated fields columns #218

Closed listeroabsoluto closed 12 years ago

listeroabsoluto commented 12 years ago

I need this feature.

Item.count Item.price Item.total <--- calculated

Item.total is not persisted, must be calculate by performing a operation between Item.count * Item.price

Is posible do this with the current versión, showing a new column TOTAL, writing a getter in Item entity, but what about aggregated fields?

This is my real problem:

/**
 *
 * @var type 
 * @ORM\OneToMany(targetEntity="\WebFactory\SaleBundle\Entity\Charge", mappedBy="cashbox")
 * @Grid\Column(title="Charges", field="charges.ammount:sum")
 */
private $charges;

/**
 *
 * @var type 
 * @ORM\OneToMany(targetEntity="\WebFactory\PurchaseBundle\Entity\PartialDischarge", mappedBy="cashbox")
 * @Grid\Column(title="Partial Discharge", field="partialDischarges.ammount:sum")
 */
private $partialDischarges;

I need a new Grid\Column with this diference: charges.ammount:sum - partialDischarges.ammount:sum

Any ideas?

Thanks.

Abhoryo commented 12 years ago

You can create your own DQL function to perfom these calculations.

OR

Define charges.ammount:sum and partialDischarges.ammount:sum with source=true and visible=false, create a new column and use the maipulateRow feature:

<?php
$source->manipulateRow(function ($row) {
    return $row->setField('diff', $row->getField('charges.ammount:sum') - $row->getField('partialDischarges.ammount:sum'));
});

$grid->addColumn(new BlankColumn(['id' => 'diff', 'title' => 'diff','size' => '30']))

OR

Don't define charges.ammount:sum and partialDischarges.ammount:sum, groupBy on the good column, create a new column 'diff' and use the maipulateQuery feature:

Exemple:

<?php
$tableAlias = $source::TABLE_ALIAS;
$source->manipulateQuery(function ($query) use ($tableAlias) {
    $query->addSelect('SUM((11 - IfNull('.$tableAlias.'.rank, 11))) as points');
});

$grid->addColumn(new BlankColumn(['id' => 'points', 'title' => 'points','size' => '30']))
Abhoryo commented 12 years ago

It's good for you ?

listeroabsoluto commented 12 years ago

I think, this approach es excellent. I have implement your solution in my codes, is very flexible.

In any case, this logic must be in controllers? This is a presentation layer scope or a data object (calculated field) logic?

Abhoryo commented 12 years ago

This logic must be in controllers?

Yes

This is a presentation layer scope or a data object (calculated field) logic?

Presentation