Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
During discussion on github, it became apparent that buffering results by default is not a good idea. Split the buffering out into a subclass that is optionally enabled using Query::bufferResults(). Do some refactoring in ResultSet to make fewer method calls and less code overall.
- Loading branch information
Showing
4 changed files
with
134 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/** | ||
* PHP Version 5.4 | ||
* | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v 3.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace Cake\ORM; | ||
|
||
/** | ||
* Buffered ResultSets differ from un-buffered ResultSets in a few ways. | ||
* | ||
* - They can be iterated multiple times. | ||
* - They can be both cached and iterated using the same object. | ||
*/ | ||
class BufferedResultSet extends ResultSet { | ||
|
||
/** | ||
* Rewind the ResultSet | ||
* | ||
* @return void | ||
*/ | ||
public function rewind() { | ||
$this->_index = 0; | ||
$this->_lastIndex = -1; | ||
} | ||
|
||
/** | ||
* Fetch a result and buffer the fetched row. | ||
* | ||
* @return mixed | ||
*/ | ||
public function valid() { | ||
$result = parent::valid(); | ||
if (!isset($this->_results[$this->_index])) { | ||
$this->_results[] = $this->_current; | ||
} | ||
return $result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* PHP Version 5.4 | ||
* | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v 3.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace Cake\Test\TestCase\ORM; | ||
|
||
use Cake\Core\Configure; | ||
use Cake\Model\ConnectionManager; | ||
use Cake\ORM\Query; | ||
use Cake\ORM\BufferedResultSet; | ||
use Cake\ORM\Table; | ||
use Cake\TestSuite\TestCase; | ||
|
||
/** | ||
* BufferedResultSet test case. | ||
*/ | ||
class BufferedResultSetTest extends TestCase { | ||
|
||
public $fixtures = ['core.article']; | ||
|
||
public function setUp() { | ||
parent::setUp(); | ||
$this->connection = ConnectionManager::getDataSource('test'); | ||
$this->table = new Table(['table' => 'articles', 'connection' => $this->connection]); | ||
} | ||
|
||
/** | ||
* Test that result sets can be rewound and re-used. | ||
* | ||
* @return void | ||
*/ | ||
public function testRewind() { | ||
$query = $this->table->find('all'); | ||
$results = $query->bufferResults()->execute(); | ||
$first = $second = []; | ||
foreach ($results as $result) { | ||
$first[] = $result; | ||
} | ||
foreach ($results as $result) { | ||
$second[] = $result; | ||
} | ||
$this->assertEquals($first, $second); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters