Browse Source

Merge pull request #5361 from cakephp/3.0-resulset-first

Handling ResultSet::first() through collection functions
Mark Story 11 years ago
parent
commit
bfa5584683
2 changed files with 13 additions and 27 deletions
  1. 0 27
      src/ORM/ResultSet.php
  2. 13 0
      tests/TestCase/ORM/QueryRegressionTest.php

+ 0 - 27
src/ORM/ResultSet.php

@@ -248,33 +248,6 @@ class ResultSet implements ResultSetInterface {
 	}
 
 /**
- * Returns the first result in this set and blocks the set so that no other
- * results can be fetched.
- *
- * When using serialized results, the index will be incremented past the
- * end of the results simulating the behavior when the result set is backed
- * by a statement.
- *
- * @return array|object|null
- */
-	public function first() {
-		if (isset($this->_results[0])) {
-			return $this->_results[0];
-		}
-
-		if ($this->valid()) {
-			if ($this->_statement) {
-				$this->_statement->closeCursor();
-			}
-			if (!$this->_statement && $this->_results) {
-				$this->_index = count($this->_results);
-			}
-			return $this->_current;
-		}
-		return null;
-	}
-
-/**
  * Gives the number of rows in the result set.
  *
  * Part of the Countable interface.

+ 13 - 0
tests/TestCase/ORM/QueryRegressionTest.php

@@ -563,4 +563,17 @@ class QueryRegressionTest extends TestCase {
 		$this->assertCount(3, $results);
 	}
 
+/**
+ * Tests that calling first on the query results will not remove all other results
+ * from the set.
+ *
+ * @return void
+ */
+	public function testFirstOnResultSet() {
+		$results = TableRegistry::get('Articles')->find()->all();
+		$this->assertEquals(3, $results->count());
+		$this->assertNotNull($results->first());
+		$this->assertCount(3, $results->toArray());
+	}
+
 }