Example #1
0
 /**
  * Get DataTable response
  *
  * @param Response $response DataTable response object
  * @param Request  $request  DataTable request object
  *
  * @return void
  */
 public function getResponse(Response $response, Request $request)
 {
     $this->prepareSearch($request);
     $this->preparePaging($request);
     $this->prepareOrder($request);
     $data = [];
     /** @var $entity Entity */
     foreach ($this->query as $entity) {
         $row = [];
         foreach ($this->table->getColumns() as $column) {
             if ($column instanceof ColumnInterface) {
                 $row[$column->getData()] = $column->getContent($entity);
                 continue;
             }
             $property = $this->getProperty($column->getData());
             $columnData = explode('.', $column->getData());
             $tableAlias = $columnData[0];
             $colName = $columnData[1];
             /**
              * If property is association
              */
             if (is_array($property)) {
                 if (is_callable($column->getFormatter())) {
                     $row[$tableAlias][$colName] = $this->doFormatter($column->getFormatter(), [$entity->get($property['propertyPath']) instanceof Entity ? $entity->get($property['propertyPath'])->get($property['field']) : $entity->get($property['propertyPath']), $entity, $entity->get($property['propertyPath'])]);
                 } else {
                     if ($entity->get($property['propertyPath']) instanceof Entity) {
                         $row[$tableAlias][$colName] = (string) $entity->get($property['propertyPath'])->get($property['field']);
                         /**
                          * BelongsToMany Association
                          */
                     } elseif (is_array($entity->get($property['propertyPath']))) {
                         $output = [];
                         /** @var Entity $belongsToManyEntity */
                         foreach ($entity->get($property['propertyPath']) as $belongsToManyEntity) {
                             $output[] = $belongsToManyEntity->get($property['field']);
                         }
                         $row[$tableAlias][$colName] = implode(', ', $output);
                     }
                 }
             } else {
                 if (is_callable($column->getFormatter())) {
                     $row[$tableAlias][$colName] = $this->doFormatter($column->getFormatter(), [$entity->get($property), $entity]);
                 } else {
                     $row[$tableAlias][$colName] = (string) $entity->get($property);
                 }
             }
         }
         $data[] = $row;
     }
     $response->setDraw($request->getDraw())->setRecordsTotal($this->clonedQuery->count())->setRecordsFiltered(self::$countBeforePaging)->setData($data);
 }