Skip to content

Commit

Permalink
Database\Table: column refetch does not modify Selection [closes #1356]
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Jan 15, 2014
1 parent 6359656 commit b2bf9b6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
29 changes: 19 additions & 10 deletions Nette/Database/Table/Selection.php
Expand Up @@ -634,26 +634,35 @@ public function accessColumn($key, $selectColumn = TRUE)
}

if ($selectColumn && !$this->sqlBuilder->getSelect() && $this->previousAccessedColumns && ($key === NULL || !isset($this->previousAccessedColumns[$key]))) {
$this->previousAccessedColumns = array();

if ($this->sqlBuilder->getLimit()) {
$generalCacheKey = $this->generalCacheKey;
$primaries = array();
$sqlBuilder = $this->sqlBuilder;

$primaryValues = array();
foreach ((array) $this->rows as $row) {
$primary = $row->getPrimary();
$primaries[] = is_array($primary) ? array_values($primary) : $primary;
$primaryValues[] = is_array($primary) ? array_values($primary) : $primary;
}
}
$this->previousAccessedColumns = array();
$this->emptyResultSet(FALSE);
if ($this->sqlBuilder->getLimit()) {

$this->emptyResultSet(FALSE);
$this->sqlBuilder = clone $this->sqlBuilder;
$this->sqlBuilder->setLimit(NULL, NULL);
$this->wherePrimary($primaries);
$this->wherePrimary($primaryValues);

$this->generalCacheKey = $generalCacheKey;
$this->execute();
$this->sqlBuilder = $sqlBuilder;
} else {
$this->emptyResultSet(FALSE);
$this->execute();
}

$this->dataRefreshed = TRUE;

if ($key === NULL) {
// we need to move iterator in resultset
$this->execute();
// move iterator to specific key
if (isset($currentKey)) {
while (key($this->data) !== $currentKey) {
next($this->data);
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Nette/Database/Table/bugs/bug1356.phpt
@@ -0,0 +1,27 @@
<?php

/**
* Test: bug 1356
*
* @author Jan Skrasek
* @dataProvider? ../../databases.ini
*/

use Tester\Assert;

require __DIR__ . '/../../connect.inc.php';

Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../../files/{$driverName}-nette_test1.sql");


$books = $context->table('book')->limit(1);
foreach ($books as $book) $book->id;
$books->__destruct();


$books = $context->table('book')->limit(1);
foreach ($books as $book) {
$book->title;
}

Assert::same(reformat('SELECT * FROM [book] LIMIT 1'), $books->getSql());

0 comments on commit b2bf9b6

Please sign in to comment.