/** * * @param type $columns * @param type $page * @param type $limit * @param type $maxResults * @param type $gridDataJunction * @return \APY\DataGridBundle\Grid\Rows */ public function execute($columns, $page = 0, $limit = 0, $maxResults = null, $gridDataJunction = Column::DATA_CONJUNCTION) { $this->query = $this->getQueryBuilder(); $this->querySelectfromSource = clone $this->query; $bindIndex = 123; $serializeColumns = array(); $where = $gridDataJunction === Column::DATA_CONJUNCTION ? $this->query->expr()->andx() : $this->query->expr()->orx(); foreach ($columns as $column) { if (!in_array($column->getId(), $this->excludedColumns)) { $fieldName = $this->getFieldName($column, true); $this->query->addSelect($fieldName); $this->querySelectfromSource->addSelect($fieldName); if ($column->isSorted()) { $this->query->orderBy($this->getFieldName($column), $column->getOrder()); } if ($column->isFiltered()) { // Some attributes of the column can be changed in this function $filters = $column->getFilters('entity'); $isDisjunction = $column->getDataJunction() === Column::DATA_DISJUNCTION; $hasHavingClause = $column->hasDQLFunction(); $sub = $isDisjunction ? $this->query->expr()->orx() : ($hasHavingClause ? $this->query->expr()->andx() : $where); foreach ($filters as $filter) { // \Doctrine\Common\Util\Debug::dump($column); $operator = $this->normalizeOperator($filter->getOperator()); $q = $this->query->expr()->{$operator}($this->getFieldName($column, false), "?{$bindIndex}"); if ($filter->getOperator() == Column::OPERATOR_NLIKE) { $q = $this->query->expr()->not($q); } $sub->add($q); if ($filter->getValue() !== null) { $this->query->setParameter($bindIndex++, $this->normalizeValue($filter->getOperator(), $filter->getValue())); } } if ($hasHavingClause) { $this->query->andHaving($sub); } elseif ($isDisjunction) { $where->add($sub); } } if ($column->getType() === 'array') { $serializeColumns[] = $column->getId(); } } } if ($where->count() > 0) { //Using ->andWhere here to make sure we preserve any other where clauses present in the query builder //the other where clauses may have come from an external builder $this->query->andWhere($where); } foreach ($this->joins as $alias => $field) { if (null !== $field['type'] && strtolower($field['type']) === 'inner') { $join = 'join'; } else { $join = 'leftJoin'; } $this->query->{$join}($field['field'], $alias); $this->querySelectfromSource->{$join}($field['field'], $alias); } if ($page > 0) { $this->query->setFirstResult($page * $limit); } if ($limit > 0) { if ($maxResults !== null && $maxResults - $page * $limit < $limit) { $limit = $maxResults - $page * $limit; } $this->query->setMaxResults($limit); } elseif ($maxResults !== null) { $this->query->setMaxResults($maxResults); } if (!empty($this->groupBy)) { $this->query->resetDQLPart('groupBy'); $this->querySelectfromSource->resetDQLPart('groupBy'); foreach ($this->groupBy as $field) { $this->query->addGroupBy($this->getGroupByFieldName($field)); $this->querySelectfromSource->addGroupBy($this->getGroupByFieldName($field)); } } if ($this->groupById === true) { $this->query->addGroupBy($this->getGroupByFieldName('id')); } //call overridden prepareQuery or associated closure $this->prepareQuery($this->query); $query = $this->query->getQuery(); foreach ($this->hints as $hintKey => $hintValue) { $query->setHint($hintKey, $hintValue); } $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'TMSolution\\DataGridBundle\\Walker\\MysqlWalker'); $query->setHint("mysqlWalker.count", true); $items = $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY); $this->prepareTotalCount(); // $oids = array(); // foreach ($items as $item) { // // $oid = ObjectIdentity::fromDomainObject($item); // $oids[] = $oid; // } // // $this->aclProvider->findAcls($oids); // preload Acls from database // // foreach ($items as $item) { // if (false === $this->securityContext->isGranted('VIEW', $item)) { // // denied // throw new AccessDeniedException(); // } // } // var_dump($items); $repository = $this->manager->getRepository($this->entityName); // Force the primary field to get the entity in the manipulatorRow $primaryColumnId = null; foreach ($columns as $column) { if ($column->isPrimary()) { $primaryColumnId = $column->getId(); break; } } // hydrate result $result = new Rows(); foreach ($items as $item) { $row = new Row(); foreach ($item as $key => $value) { $key = str_replace('::', '.', $key); if (in_array($key, $serializeColumns) && is_string($value)) { //echo $value."\n"; @($value = unserialize($value)); } $row->setField($key, $value); } $row->setPrimaryField($primaryColumnId); //Setting the representative repository for entity retrieving $row->setRepository($repository); //call overridden prepareRow or associated closure if (($modifiedRow = $this->prepareRow($row)) != null) { $result->addRow($modifiedRow); } } return $result; }
/** * @param \APY\DataGridBundle\Grid\Column\Column[] $columns * @param int $page Page Number * @param int $limit Rows Per Page * @param int $gridDataJunction Grid data junction * @return \APY\DataGridBundle\Grid\Rows */ public function execute($columns, $page = 0, $limit = 0, $maxResults = null, $gridDataJunction = Column::DATA_CONJUNCTION) { $this->query = $this->getQueryBuilder(); $this->querySelectfromSource = clone $this->query; $bindIndex = 123; $serializeColumns = array(); $where = $gridDataJunction === Column::DATA_CONJUNCTION ? $this->query->expr()->andx() : $this->query->expr()->orx(); $columnsById = array(); foreach ($columns as $column) { $columnsById[$column->getId()] = $column; } foreach ($columns as $column) { // If a column is a manual field, ie a.col*b.col as myfield, it is added to select from user. if ($column->getIsManualField() === false) { $fieldName = $this->getFieldName($column, true); $this->query->addSelect($fieldName); $this->querySelectfromSource->addSelect($fieldName); } if ($column->isSorted()) { if ($column->getType() === 'join') { $this->query->resetDQLPart('orderBy'); foreach ($column->getJoinColumns() as $columnName) { $this->query->addOrderBy($this->getFieldName($columnsById[$columnName]), $column->getOrder()); } } else { $this->query->orderBy($this->getFieldName($column), $column->getOrder()); } } if ($column->isFiltered()) { // Some attributes of the column can be changed in this function $filters = $column->getFilters('entity'); $isDisjunction = $column->getDataJunction() === Column::DATA_DISJUNCTION; $hasHavingClause = $column->hasDQLFunction() || $column->getIsAggregate(); $sub = $isDisjunction ? $this->query->expr()->orx() : ($hasHavingClause ? $this->query->expr()->andx() : $where); /** @var Filter $filter */ foreach ($filters as $filter) { $operator = $this->normalizeOperator($filter->getOperator()); $columnForFilter = $column->getType() !== 'join' ? $column : $columnsById[$filter->getColumnName()]; $fieldName = $this->getFieldName($columnForFilter, false); $bindIndexPlaceholder = "?{$bindIndex}"; if (in_array($filter->getOperator(), array(Column::OPERATOR_LIKE, Column::OPERATOR_RLIKE, Column::OPERATOR_LLIKE, Column::OPERATOR_NLIKE))) { $fieldName = "LOWER({$fieldName})"; $bindIndexPlaceholder = "LOWER({$bindIndexPlaceholder})"; } $q = $this->query->expr()->{$operator}($fieldName, $bindIndexPlaceholder); if ($filter->getOperator() == Column::OPERATOR_NLIKE || $filter->getOperator() == Column::OPERATOR_NSLIKE) { $q = $this->query->expr()->not($q); } $sub->add($q); if ($filter->getValue() !== null) { $this->query->setParameter($bindIndex++, $this->normalizeValue($filter->getOperator(), $filter->getValue())); } } if (method_exists($column, 'addFilterCondition')) { $column->addFilterCondition($sub, $this->query); } if ($hasHavingClause) { $this->query->andHaving($sub); } elseif ($isDisjunction) { $where->add($sub); } } if ($column->getType() === 'array') { $serializeColumns[] = $column->getId(); } } if ($where->count() > 0) { //Using ->andWhere here to make sure we preserve any other where clauses present in the query builder //the other where clauses may have come from an external builder $this->query->andWhere($where); } foreach ($this->joins as $alias => $field) { if (null !== $field['type'] && strtolower($field['type']) === 'inner') { $join = 'join'; } else { $join = 'leftJoin'; } $this->query->{$join}($field['field'], $alias); $this->querySelectfromSource->{$join}($field['field'], $alias); } if ($page > 0) { $this->query->setFirstResult($page * $limit); } if ($limit > 0) { if ($maxResults !== null && $maxResults - $page * $limit < $limit) { $limit = $maxResults - $page * $limit; } $this->query->setMaxResults($limit); } elseif ($maxResults !== null) { $this->query->setMaxResults($maxResults); } if (!empty($this->groupBy)) { $this->query->resetDQLPart('groupBy'); $this->querySelectfromSource->resetDQLPart('groupBy'); foreach ($this->groupBy as $field) { $this->query->addGroupBy($this->getGroupByFieldName($field)); $this->querySelectfromSource->addGroupBy($this->getGroupByFieldName($field)); } } //call overridden prepareQuery or associated closure $this->prepareQuery($this->query); $hasJoin = $this->checkIfQueryHasFetchJoin($this->query); $query = $this->query->getQuery(); foreach ($this->hints as $hintKey => $hintValue) { $query->setHint($hintKey, $hintValue); } $items = new Paginator($query, $hasJoin); $repository = $this->manager->getRepository($this->entityName); // Force the primary field to get the entity in the manipulatorRow $primaryColumnId = null; foreach ($columns as $column) { if ($column->isPrimary()) { $primaryColumnId = $column->getId(); break; } } // hydrate result $result = new Rows(); foreach ($items as $item) { $row = new Row(); foreach ($item as $key => $value) { $key = str_replace('::', '.', $key); if (in_array($key, $serializeColumns) && is_string($value)) { $value = unserialize($value); } $row->setField($key, $value); } $row->setPrimaryField($primaryColumnId); //Setting the representative repository for entity retrieving $row->setRepository($repository); //call overridden prepareRow or associated closure if (($modifiedRow = $this->prepareRow($row)) != null) { $result->addRow($modifiedRow); } } return $result; }
/** * @param \APY\DataGridBundle\Grid\Column\Column[] $columns * @param int $page Page Number * @param int $limit Rows Per Page * @param int $gridDataJunction Grid data junction * @return \APY\DataGridBundle\Grid\Rows */ public function execute($columns, $page = 0, $limit = 0, $maxResults = null, $gridDataJunction = Column::DATA_CONJUNCTION) { $this->query = $this->manager->createQueryBuilder($this->class); $this->query->from($this->class, self::TABLE_ALIAS); $this->querySelectfromSource = clone $this->query; $bindIndex = 123; $serializeColumns = array(); $where = $gridDataJunction === Column::DATA_CONJUNCTION ? $this->query->expr()->andx() : $this->query->expr()->orx(); foreach ($columns as $column) { $fieldName = $this->getFieldName($column, true); $this->query->addSelect($fieldName); $this->querySelectfromSource->addSelect($fieldName); if ($column->isSorted()) { $this->query->orderBy($this->getFieldName($column), $column->getOrder()); } if ($column->isFiltered()) { // Some attributes of the column can be changed in this function $filters = $column->getFilters('entity'); $isDisjunction = $column->getDataJunction() === Column::DATA_DISJUNCTION; $hasHavingClause = $column->hasDQLFunction(); $sub = $isDisjunction ? $this->query->expr()->orx() : ($hasHavingClause ? $this->query->expr()->andx() : $where); foreach ($filters as $filter) { $operator = $this->normalizeOperator($filter->getOperator()); $q = $this->query->expr()->{$operator}($this->getFieldName($column, false), "?{$bindIndex}"); if (in_array($filter->getOperator(), Column::$virtualNotOperators)) { $q = $this->query->expr()->not($q); } $sub->add($q); if ($filter->getValue() !== null) { $this->query->setParameter($bindIndex++, $this->normalizeValue($filter->getOperator(), $filter->getValue())); } } if ($hasHavingClause) { $this->query->having($sub); } elseif ($isDisjunction) { $where->add($sub); } } // Still useful? if ($column->getType() === 'array') { $serializeColumns[] = $column->getId(); } } if ($where->count() > 0) { $this->query->where($where); } foreach ($this->joins as $alias => $field) { $join = null !== $field['type'] && strtolower($field['type']) === 'inner' ? 'join' : 'leftJoin'; $this->query->{$join}($field['field'], $alias); $this->querySelectfromSource->{$join}($field['field'], $alias); } if ($page > 0) { $this->query->setFirstResult($page * $limit); } if ($limit > 0) { if ($maxResults !== null && $maxResults - $page * $limit < $limit) { $limit = $maxResults - $page * $limit; } $this->query->setMaxResults($limit); } elseif ($maxResults !== null) { $this->query->setMaxResults($maxResults); } if (!empty($this->groupBy)) { $this->query->resetDQLPart('groupBy'); $this->querySelectfromSource->resetDQLPart('groupBy'); foreach ($this->groupBy as $field) { $this->query->addGroupBy($this->getGroupByFieldName($field)); $this->querySelectfromSource->addGroupBy($this->getGroupByFieldName($field)); } } //call overridden prepareQuery or associated closure $this->prepareQuery($this->query); $query = $this->query->getQuery(); foreach ($this->hints as $hintKey => $hintValue) { $query->setHint($hintKey, $hintValue); } $items = $query->getResult(); $repository = $this->manager->getRepository($this->entityName); // Force the primary field to get the entity in the manipulatorRow $primaryColumnId = null; foreach ($columns as $column) { if ($column->isPrimary()) { $primaryColumnId = $column->getId(); break; } } // hydrate result $result = new Rows(); foreach ($items as $item) { $row = new Row(); foreach ($item as $key => $value) { $key = str_replace('::', '.', $key); if (in_array($key, $serializeColumns) && is_string($value)) { $value = unserialize($value); } $row->setField($key, $value); } $row->setPrimaryField($primaryColumnId); //Setting the representative repository for entity retrieving $row->setRepository($repository); //call overridden prepareRow or associated closure if (($modifiedRow = $this->prepareRow($row)) != null) { $result->addRow($modifiedRow); } } return $result; }