From 4a89017868958b545d33bf43eb7f60d74b626335 Mon Sep 17 00:00:00 2001 From: ph Date: Sun, 6 Sep 2015 09:33:45 +0200 Subject: [PATCH 1/2] tests: related query count --- tests/Database/Table/bugs/query.count.phpt | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/Database/Table/bugs/query.count.phpt diff --git a/tests/Database/Table/bugs/query.count.phpt b/tests/Database/Table/bugs/query.count.phpt new file mode 100644 index 000000000..4f281d8ab --- /dev/null +++ b/tests/Database/Table/bugs/query.count.phpt @@ -0,0 +1,36 @@ +query("INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (1, 24, 'private');"); +$context->query("INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (2, 24, 'private');"); +$context->query("INSERT INTO book_tag_alt (book_id, tag_id, state) VALUES (2, 22, 'private');"); + +test(function () use ($connection, $context) { + + $context->table('author')->get(11); // have to build cache first + + $count = 0; + $connection->onQuery[] = function() use (&$count) { + $count ++; + }; + + foreach ($context->table('book') as $book) { + foreach ($book->related('book_tag_alt')->where('state', 'private') as $bookTag) { + $tag = $bookTag->tag; + } + } + + Assert::same(3, $count); + + +}); From 589ff757d729c0820274f0dac3095dfae7ec1322 Mon Sep 17 00:00:00 2001 From: Hynek Vilimek Date: Tue, 8 Sep 2015 10:34:30 +0200 Subject: [PATCH 2/2] Selection: referenced cache cleared only for root selection (not in GroupedSelection), performance improvment --- src/Database/Table/GroupedSelection.php | 5 +++++ src/Database/Table/Selection.php | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Database/Table/GroupedSelection.php b/src/Database/Table/GroupedSelection.php index fffdaed64..47b22f3f7 100644 --- a/src/Database/Table/GroupedSelection.php +++ b/src/Database/Table/GroupedSelection.php @@ -201,6 +201,11 @@ protected function loadRefCache() } } + protected function emptyResultSet($saveCache = TRUE, $deleteRererencedCache = TRUE) + { + parent::emptyResultSet($saveCache, false); + } + /********************* manipulation ****************d*g**/ diff --git a/src/Database/Table/Selection.php b/src/Database/Table/Selection.php index 296b3c97e..d85e4427a 100644 --- a/src/Database/Table/Selection.php +++ b/src/Database/Table/Selection.php @@ -543,7 +543,7 @@ protected function query($query) } - protected function emptyResultSet($saveCache = TRUE) + protected function emptyResultSet($saveCache = TRUE, $deleteRererencedCache = TRUE) { if ($this->rows !== NULL && $saveCache) { $this->saveCacheState(); @@ -558,7 +558,9 @@ protected function emptyResultSet($saveCache = TRUE) $this->specificCacheKey = NULL; $this->generalCacheKey = NULL; $this->refCache['referencingPrototype'] = []; - $this->refCache['referenced'] = []; + if ($deleteRererencedCache) { + $this->refCache['referenced'] = []; + } }