Browse Source

Fixing various faling tests. The problem was not closing the statements, among other things

Jose Lorenzo Rodriguez 12 years ago
parent
commit
70e3ef04cf

+ 2 - 1
src/Database/Expression/TupleComparison.php

@@ -88,7 +88,8 @@ class TupleComparison extends Comparison {
 			$type = $this->_type;
 			$multiType = is_array($type);
 			$isMulti = $this->isMulti($i, $type);
-			$type = $isMulti ? str_replace('[]', '', $type) : $type;
+			$type = $multiType ? $type : str_replace('[]', '', $type);
+			$type = $type ?: null;
 
 			if ($isMulti) {
 				$bound = [];

+ 1 - 0
src/ORM/Association/ExternalAssociationTrait.php

@@ -313,6 +313,7 @@ trait ExternalAssociationTrait {
  */
 	protected function _buildSubquery($query, $foreignKey) {
 		$filterQuery = clone $query;
+		$filterQuery->limit(null);
 		$filterQuery->contain([], true);
 		$joins = $filterQuery->join();
 		foreach ($joins as $i => $join) {

+ 1 - 2
src/ORM/Query.php

@@ -780,8 +780,7 @@ class Query extends DatabaseQuery {
 		if ($this->_dirty) {
 			$this->limit(1);
 		}
-		$this->_results = $this->all();
-		return $this->_results->first();
+		return $this->all()->first();
 	}
 
 /**

+ 18 - 3
src/ORM/ResultSet.php

@@ -114,6 +114,13 @@ class ResultSet implements Countable, Iterator, Serializable, JsonSerializable {
 	protected $_useBuffering = true;
 
 /**
+ * Holds the count of records in this result set
+ *
+ * @var integer
+ */
+	protected $_count;
+
+/**
  * Constructor
  *
  * @param Query from where results come
@@ -128,6 +135,10 @@ class ResultSet implements Countable, Iterator, Serializable, JsonSerializable {
 		$this->_hydrate = $this->_query->hydrate();
 		$this->_entityClass = $query->repository()->entityClass();
 		$this->_useBuffering = $query->bufferResults();
+
+		if ($statement) {
+			$this->count();
+		}
 	}
 
 /**
@@ -199,8 +210,9 @@ class ResultSet implements Countable, Iterator, Serializable, JsonSerializable {
 
 		$this->_current = $this->_fetchResult();
 		$valid = $this->_current !== false;
+		$hasNext = $this->_index < $this->_count;
 
-		if (!$valid && $this->_statement) {
+		if ($this->_statement && !($valid && $hasNext)) {
 			$this->_statement->closeCursor();
 		}
 
@@ -271,10 +283,13 @@ class ResultSet implements Countable, Iterator, Serializable, JsonSerializable {
  * @return integer
  */
 	public function count() {
+		if ($this->_count !== null) {
+			return $this->_count;
+		}
 		if ($this->_statement) {
-			return $this->_statement->rowCount();
+			return $this->_count = $this->_statement->rowCount();
 		}
-		return count($this->_results);
+		return $this->_count = count($this->_results);
 	}
 
 /**

+ 1 - 0
tests/TestCase/Model/Behavior/TranslateBehaviorTest.php

@@ -426,6 +426,7 @@ class TranslateBehaviorTest extends TestCase {
 
 		$results = $table->find()
 			->select(['title', 'body'])
+			->order(['title' => 'asc'])
 			->contain(['Authors' => function($q) {
 				return $q->select(['id', 'name']);
 			}]);

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

@@ -885,7 +885,10 @@ class QueryTest extends TestCase {
 		$params = [$this->connection, $this->table];
 		$query = $this->getMock('\Cake\ORM\Query', ['execute'], $params);
 
-		$statement = $this->getMock('\Database\StatementInterface', ['fetch', 'closeCursor']);
+		$statement = $this->getMock(
+			'\Database\StatementInterface',
+			['fetch', 'closeCursor', 'rowCount']
+		);
 		$statement->expects($this->exactly(3))
 			->method('fetch')
 			->will($this->onConsecutiveCalls(['a' => 1], ['a' => 2], false));