From 0cc652636dc2858a4cd3d5dc606a068829244815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C4=8Cervenka?= Date: Fri, 6 Mar 2015 13:26:06 +0100 Subject: [PATCH] Table: fixed Selection::getReferencedTable() always refetching when primary is NULL --- src/Database/Table/Selection.php | 2 +- tests/Database/Table/Table.ref().phpt | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Database/Table/Selection.php b/src/Database/Table/Selection.php index 1eda82fa5..e459ac08c 100644 --- a/src/Database/Table/Selection.php +++ b/src/Database/Table/Selection.php @@ -846,7 +846,7 @@ public function getReferencedTable(ActiveRow $row, $table, $column = NULL) $referenced = & $this->refCache['referenced'][$this->getSpecificCacheKey()]["$table.$column"]; $selection = & $referenced['selection']; $cacheKeys = & $referenced['cacheKeys']; - if ($selection === NULL || !isset($cacheKeys[$checkPrimaryKey])) { + if ($selection === NULL || ($checkPrimaryKey !== NULL && !isset($cacheKeys[$checkPrimaryKey]))) { $this->execute(); $cacheKeys = array(); foreach ($this->rows as $row) { diff --git a/tests/Database/Table/Table.ref().phpt b/tests/Database/Table/Table.ref().phpt index 31d640a74..63135207f 100644 --- a/tests/Database/Table/Table.ref().phpt +++ b/tests/Database/Table/Table.ref().phpt @@ -31,3 +31,20 @@ test(function() use ($context) { test(function() use ($context) { Assert::null($context->table('book')->get(2)->ref('author', 'translator_id')); }); + +test(function() use ($context, $connection) { + $counter = 0; + + $connection->onQuery[] = function($connection, $result) use (&$counter) { + $counter++; + }; + + $table = $context->table('book'); + + $names = array(); + foreach ($table as $book) { + $translator = $book->ref('author', 'translator_id'); + } + + Assert::equal(2, $counter); +});