Browse Source

Adds Query::clearResult().

Clears the internal resultset and count value.

Closes #14564
Frank de Graaf (Phally) 5 years ago
parent
commit
c1cfe0251e
2 changed files with 41 additions and 0 deletions
  1. 13 0
      src/ORM/Query.php
  2. 28 0
      tests/TestCase/ORM/QueryTest.php

+ 13 - 0
src/ORM/Query.php

@@ -879,6 +879,19 @@ class Query extends DatabaseQuery implements JsonSerializable, QueryInterface
     }
 
     /**
+     * Clears the internal result cache and the internal count value from the current
+     * query object.
+     *
+     * @return $this
+     */
+    public function clearResult()
+    {
+        $this->_dirty();
+
+        return $this;
+    }
+
+    /**
      * Object clone hook.
      *
      * Destroys the clones inner iterator and clones the value binder, and eagerloader instances.

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

@@ -859,6 +859,34 @@ class QueryTest extends TestCase
     }
 
     /**
+     * Test clearResult()
+     *
+     * @return void
+     */
+    public function testClearResult()
+    {
+        $article = $this->getTableLocator()->get('articles');
+        $query = new Query($this->connection, $article);
+
+        $firstCount = $query->count();
+        $firstResults = $query->toList();
+
+        $this->assertEquals(3, $firstCount);
+        $this->assertCount(3, $firstResults);
+
+        $article->delete(reset($firstResults));
+        $return = $query->clearResult();
+
+        $this->assertSame($return, $query);
+
+        $secondCount = $query->count();
+        $secondResults = $query->toList();
+
+        $this->assertEquals(2, $secondCount);
+        $this->assertCount(2, $secondResults);
+    }
+
+    /**
      * Tests that applying array options to a query will convert them
      * to equivalent function calls with the correspondent array values
      *