Browse Source

Implemented prepending of query formatters, this way behaviors have
a bit more control over formatters they attach

Jose Lorenzo Rodriguez 12 years ago
parent
commit
da2f38f160
2 changed files with 28 additions and 3 deletions
  1. 24 3
      src/ORM/Query.php
  2. 4 0
      tests/TestCase/ORM/QueryTest.php

+ 24 - 3
src/ORM/Query.php

@@ -34,6 +34,21 @@ use Cake\ORM\Table;
 class Query extends DatabaseQuery {
 
 /**
+ * Indicates that the operation should append to the list
+ */
+	const APPEND = 0;
+
+/**
+ * Indicates that the operation should prepend to the list
+ */
+	const PREPEND = 1;
+
+/**
+ * Indicates that the operation should overwrite the list
+ */
+	const OVERWRITE = true;
+
+/**
  * Instance of a table object this query is bound to
  *
  * @var \Cake\ORM\Table
@@ -841,16 +856,22 @@ class Query extends DatabaseQuery {
  * }}}
  *
  * @param callable $formatter
- * @param boolean $overwrite
+ * @param boolean|integer $mode
  * @return Cake\ORM\Query|array
  */
-	public function formatResults(callable $formatter = null, $overwrite = false) {
-		if ($overwrite) {
+	public function formatResults(callable $formatter = null, $mode = self::APPEND) {
+		if ($mode === self::OVERWRITE) {
 			$this->_formatters = [];
 		}
 		if ($formatter === null) {
 			return $this->_formatters;
 		}
+
+		if ($mode === self::PREPEND) {
+			array_unshift($this->_formatters, $formatter);
+			return $this;
+		}
+
 		$this->_formatters[] = $formatter;
 		return $this;
 	}

+ 4 - 0
tests/TestCase/ORM/QueryTest.php

@@ -1769,6 +1769,10 @@ class QueryTest extends TestCase {
 		$this->assertSame([$callback2], $query->formatResults());
 		$query->formatResults(null, true);
 		$this->assertSame([], $query->formatResults());
+
+		$query->formatResults($callback1);
+		$query->formatResults($callback2, $query::PREPEND);
+		$this->assertSame([$callback2, $callback1], $query->formatResults());
 	}
 
 /**