/**
  * get grid object filled
  *
  * @param  \Doctrine\ORM\QueryBuilder                $queryBuilder
  * @param  \Kitpages\DataGridBundle\Model\GridConfig $gridConfig
  * @param  \Symfony\Component\HttpFoundation\Request $request
  * @return \Kitpages\DataGridBundle\Model\Grid
  */
 public function getGrid(QueryBuilder $queryBuilder, GridConfig $gridConfig, Request $request)
 {
     // create grid objet
     $grid = new Grid();
     $grid->setGridConfig($gridConfig);
     $grid->setUrlTool(new UrlTool());
     $grid->setRequestUri($request->getRequestUri());
     // create base request
     $gridQueryBuilder = clone $queryBuilder;
     // Apply filters
     $filter = $request->query->get($grid->getFilterFormName(), "");
     $this->applyFilter($gridQueryBuilder, $grid, $filter);
     // Apply sorting
     $sortField = $request->query->get($grid->getSortFieldFormName(), "");
     $sortOrder = $request->query->get($grid->getSortOrderFormName(), "");
     $this->applySort($gridQueryBuilder, $grid, $sortField, $sortOrder);
     // build paginator
     $paginatorConfig = $gridConfig->getPaginatorConfig();
     if ($paginatorConfig == null) {
         $paginatorConfig = new PaginatorConfig();
         $paginatorConfig->setCountFieldName($gridConfig->getCountFieldName());
         $paginatorConfig->setName($gridConfig->getName());
     }
     $paginator = $this->getPaginator($gridQueryBuilder, $paginatorConfig, $request);
     $grid->setPaginator($paginator);
     // calculate limits
     $gridQueryBuilder->setMaxResults($paginator->getPaginatorConfig()->getItemCountInPage());
     $gridQueryBuilder->setFirstResult(($paginator->getCurrentPage() - 1) * $paginator->getPaginatorConfig()->getItemCountInPage());
     // send event for changing grid query builder
     $event = new DataGridEvent();
     $event->set("grid", $grid);
     $event->set("gridQueryBuilder", $gridQueryBuilder);
     $event->set("request", $request);
     $this->dispatcher->dispatch(KitpagesDataGridEvents::ON_GET_GRID_QUERY, $event);
     if (!$event->isDefaultPrevented()) {
         // execute request
         $query = $gridQueryBuilder->getQuery();
         $event->set("query", $query);
     }
     $this->dispatcher->dispatch(KitpagesDataGridEvents::AFTER_GET_GRID_QUERY, $event);
     // hack : recover query from the event so the developper can build a new grid
     // from the gridQueryBuilder in the listener and reinject it in the event.
     $query = $event->get("query");
     // execute the query
     $itemList = $query->getArrayResult();
     // normalize result (for request of type $queryBuilder->select("item, bp, item.id * 3 as titi"); )
     $normalizedItemList = array();
     foreach ($itemList as $item) {
         $normalizedItem = array();
         foreach ($item as $key => $val) {
             if (is_int($key)) {
                 foreach ($val as $newKey => $newVal) {
                     $normalizedItem[$newKey] = $newVal;
                 }
             } else {
                 $normalizedItem[$key] = $val;
             }
         }
         $normalizedItemList[] = $normalizedItem;
     }
     // end normalization
     $grid->setItemList($normalizedItemList);
     $grid->setRootAliases($gridQueryBuilder->getRootAliases());
     return $grid;
 }