AliHichem / AliDatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities
MIT License
112 stars 72 forks source link

Select entity and convert it into the value before rendering. #94

Closed aistis- closed 9 years ago

aistis- commented 10 years ago

Hello, I need to do some calculations with my entity before converting it to string/integer. It looks something like this:

$priceProvider = $this->get('the_most_awesome_price_');
$buyer = $this->getUser();

$datatable = $this->get('datatable')
    ->setEntity('SuperAwesomeundle:WarehouseProduct', 'wp')
    ->addJoin('wp.product', 'p')
    ->addJoin('wp.warehouse', 'w')
    ->addJoin('w.groups', 'wg', Join::ON)
    ->addJoin('wg.group', 'g', Join::ON)
    ->addJoin('g.members', 'm', Join::ON)
    ->setWhere('m = :buyer', array('buyer' => $buyer))
    ->setFields(array(
        'Product'       => 'p.name',
        'Price'         => 'wp', // I need here to select the whole entity 
        'Quantity'      => 'wp.quantity',
        'Warehouse'     => 'w.name',
        '_identifier_'  => 'wp.id'
    ))
    ->setHasAction(true)
    ->setRenderers(
        array(
            4 => array(
                'view' => 'SuperAwesomeundle:RandomController:actions.html.twig',
            ),
        )
    )
    ->setRenderer(
        function(&$data) use ($priceProvider, $buyer)
        {
            foreach ($data as $key => $value){
                if (1 === $key) {
                    // I need to do some calculations from the WarehouseProduct
                    // entity here to get printable value
                    $data[$key] = $priceProvider->getPriceForBuyer($buyer, $data[$key]);
                }
            }
        }
    );

Do you guys have any ideas for that?

aistis- commented 10 years ago

My work around is to get the entity by ID int the renderer...