Skip to content

Commit

Permalink
Hadling ResultSet::first() trhough collection functions
Browse files Browse the repository at this point in the history
In the past we had to handle the first() function differently so we
could close the statement. Doe to improvemets in how the statement
is closed in te query and the connection, this is no loger required.
  • Loading branch information
lorenzo committed Dec 8, 2014
1 parent e889485 commit 902102f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
27 changes: 0 additions & 27 deletions src/ORM/ResultSet.php
Expand Up @@ -247,33 +247,6 @@ public function unserialize($serialized) {
$this->_results = unserialize($serialized);
}

/**
* 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.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -563,4 +563,17 @@ public function testSelectNoFieldsOnPrimaryAlias() {
$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());
}

}

0 comments on commit 902102f

Please sign in to comment.