Skip to content

Commit e72059c

Browse files
Hynek Vilimekdg
authored andcommitted
Selection: referenced cache cleared only for root selection (not in GroupedSelection), performance improvement [Closes #91][Closes #92]
1 parent b3b6e9b commit e72059c

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/Database/Table/GroupedSelection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ protected function loadRefCache()
202202
}
203203

204204

205+
protected function emptyResultSet($saveCache = TRUE, $deleteRererencedCache = TRUE)
206+
{
207+
parent::emptyResultSet($saveCache, FALSE);
208+
}
209+
210+
205211
/********************* manipulation ****************d*g**/
206212

207213

src/Database/Table/Selection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ protected function query($query)
543543
}
544544

545545

546-
protected function emptyResultSet($saveCache = TRUE)
546+
protected function emptyResultSet($saveCache = TRUE, $deleteRererencedCache = TRUE)
547547
{
548548
if ($this->rows !== NULL && $saveCache) {
549549
$this->saveCacheState();
@@ -558,7 +558,9 @@ protected function emptyResultSet($saveCache = TRUE)
558558
$this->specificCacheKey = NULL;
559559
$this->generalCacheKey = NULL;
560560
$this->refCache['referencingPrototype'] = [];
561-
$this->refCache['referenced'] = [];
561+
if ($deleteRererencedCache) {
562+
$this->refCache['referenced'] = [];
563+
}
562564
}
563565

564566

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/**
4+
* @dataProvider? ../../databases.ini
5+
*/
6+
7+
use Tester\Assert;
8+
9+
require __DIR__ . '/../../connect.inc.php'; // create $connection
10+
11+
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../../files/{$driverName}-nette_test1.sql");
12+
13+
// add additional tags (not relevant to other tests)
14+
$context->query("INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (1, 24, 'private');");
15+
$context->query("INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (2, 24, 'private');");
16+
$context->query("INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (2, 22, 'private');");
17+
18+
test(function () use ($connection, $context) {
19+
20+
$context->table('author')->get(11); // have to build cache first
21+
22+
$count = 0;
23+
$connection->onQuery[] = function() use (& $count) {
24+
$count++;
25+
};
26+
27+
foreach ($context->table('book') as $book) {
28+
foreach ($book->related('book_tag_alt')->where('state', 'private') as $bookTag) {
29+
$tag = $bookTag->tag;
30+
}
31+
}
32+
33+
Assert::same(3, $count);
34+
});

0 commit comments

Comments
 (0)