decorateResults() public method

Callbacks will be executed lazily, if only 3 rows are fetched for database it will called 3 times, event though there might be more rows to be fetched in the cursor. Callbacks are stacked in the order they are registered, if you wish to reset the stack the call this function with the second parameter set to true. If you wish to remove all decorators from the stack, set the first parameter to null and the second to true. ### Example $query->decorateResults(function ($row) { $row['order_total'] = $row['subtotal'] + ($row['subtotal'] * $row['tax']); return $row; });
public decorateResults ( callable | null $callback, boolean $overwrite = false )
$callback callable | null The callback to invoke when results are fetched.
$overwrite boolean Whether or not this should append or replace all existing decorators.
Beispiel #1
0
 /**
  * Tests stacking decorators for results and resetting the list of decorators
  *
  * @return void
  */
 public function testDecorateResults()
 {
     $query = new Query($this->connection);
     $result = $query->select(['id', 'title'])->from('articles')->order(['id' => 'ASC'])->decorateResults(function ($row) {
         $row['modified_id'] = $row['id'] + 1;
         return $row;
     })->execute();
     while ($row = $result->fetch('assoc')) {
         $this->assertEquals($row['id'] + 1, $row['modified_id']);
     }
     $result = $query->decorateResults(function ($row) {
         $row['modified_id']--;
         return $row;
     })->execute();
     while ($row = $result->fetch('assoc')) {
         $this->assertEquals($row['id'], $row['modified_id']);
     }
     $result = $query->decorateResults(function ($row) {
         $row['foo'] = 'bar';
         return $row;
     }, true)->execute();
     while ($row = $result->fetch('assoc')) {
         $this->assertEquals('bar', $row['foo']);
         $this->assertArrayNotHasKey('modified_id', $row);
     }
     $results = $query->decorateResults(null, true)->execute();
     while ($row = $result->fetch('assoc')) {
         $this->assertArrayNotHasKey('foo', $row);
         $this->assertArrayNotHasKey('modified_id', $row);
     }
 }
 /**
  * Generate a paging subquery for older versions of Oracle Server.
  *
  * Prior to Oracle 12 there was no equivalent to LIMIT OFFSET,
  * so a subquery must be used.
  *
  * @param \Cake\Database\Query $original The query to wrap in a subquery.
  * @param int $limit The number of rows to fetch.
  * @param int $offset The number of rows to offset.
  * @return \Cake\Database\Query Modified query object.
  */
 protected function _pagingSubquery($original, $limit, $offset)
 {
     $field = 'cake_paging_out."_cake_page_rownum_"';
     $query = clone $original;
     $query->limit(null)->offset(null);
     $outer = new Query($query->connection());
     $outer->select(['cake_paging.*', '_cake_page_rownum_' => new SimpleExpression('ROWNUM')])->from(['cake_paging' => $query]);
     $outer2 = new Query($query->connection());
     $outer2->select('*')->from(['cake_paging_out' => $outer]);
     if ($offset) {
         $outer2->where(["{$field} > " . (int) $offset]);
     }
     if ($limit) {
         $value = (int) $offset + (int) $limit;
         $outer2->where(["{$field} <= {$value}"]);
     }
     $original->decorateResults(function ($row) {
         if (isset($row['_cake_page_rownum_'])) {
             unset($row['_cake_page_rownum_']);
         }
         return $row;
     });
     return $outer2;
 }
 /**
  * Returns the passed query after rewriting the DISTINCT clause, so that drivers
  * that do not support the "ON" part can provide the actual way it should be done
  *
  * @param \Cake\Database\Query $original The query to be transformed
  * @return \Cake\Database\Query
  */
 protected function _transformDistinct($original)
 {
     if (!is_array($original->clause('distinct'))) {
         return $original;
     }
     $query = clone $original;
     $distinct = $query->clause('distinct');
     $query->distinct(false);
     $order = new OrderByExpression($distinct);
     $query->select(function ($q) use($distinct, $order) {
         $over = $q->newExpr('ROW_NUMBER() OVER')->add('(PARTITION BY')->add($q->newExpr()->add($distinct)->tieWith(','))->add($order)->add(')')->tieWith(' ');
         return ['_cake_distinct_pivot_' => $over];
     })->limit(null)->offset(null)->order([], true);
     $outer = new Query($query->connection());
     $outer->select('*')->from(['_cake_distinct_' => $query])->where(['_cake_distinct_pivot_' => 1]);
     // Decorate the original query as that is what the
     // end developer will be calling execute() on originally.
     $original->decorateResults(function ($row) {
         if (isset($row['_cake_distinct_pivot_'])) {
             unset($row['_cake_distinct_pivot_']);
         }
         return $row;
     });
     return $outer;
 }
 /**
  * Generate a paging subquery for older versions of SQLserver.
  *
  * Prior to SQLServer 2012 there was no equivalent to LIMIT OFFSET, so a subquery must
  * be used.
  *
  * @param \Cake\Database\Query $original The query to wrap in a subquery.
  * @param int $limit The number of rows to fetch.
  * @param int $offset The number of rows to offset.
  * @return \Cake\Database\Query Modified query object.
  */
 protected function _pagingSubquery($original, $limit, $offset)
 {
     $field = '_cake_paging_._cake_page_rownum_';
     $query = clone $original;
     $order = $query->clause('order') ?: new OrderByExpression('NULL');
     $query->select(['_cake_page_rownum_' => new UnaryExpression('ROW_NUMBER() OVER', $order)])->limit(null)->offset(null)->order([], true);
     $outer = new Query($query->connection());
     $outer->select('*')->from(['_cake_paging_' => $query]);
     if ($offset) {
         $outer->where(["{$field} >" => $offset]);
     }
     if ($limit) {
         $outer->where(["{$field} <=" => (int) $offset + (int) $limit]);
     }
     // Decorate the original query as that is what the
     // end developer will be calling execute() on originally.
     $original->decorateResults(function ($row) {
         if (isset($row['_cake_page_rownum_'])) {
             unset($row['_cake_page_rownum_']);
         }
         return $row;
     });
     return $outer;
 }