Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Database/Table/GroupedSelection.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ protected function loadRefCache()
}
}

protected function emptyResultSet($saveCache = TRUE, $deleteRererencedCache = TRUE)
{
parent::emptyResultSet($saveCache, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place $this->refCache['referenced'] = []; here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

running line $this->refCache['referenced'] = []; for GroupedSelection was actually the problem that i am trying to solve. This will actually break the query.count.phpt test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my previous comment, which I deleted yesterday?, was wrong. I misread the code. The previous version was probably better :| I really sorry.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the iteration in iteration. The deleting referencedCache is launched for each inner interation. This leads to increasing number of sent queries (from 3 to 5 in query.count.phpt)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any difference between your code and this solution:

class GroupedSelection

protected function emptyResultSet(..) 
{
  parent::emptyResultSet($saveCache);
  $this->refCache['referenced'] = [];
}

Where is a difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Difference:

  1. My solution:
    When emptyResultSet() is called on Selection, the $this->refCache['referenced'] = []; line is executed. (the default value of second parameter is true)

When emptyResultSet() is called on GroupedSelection, the $this->refCache['referenced'] = []; is not executed (the second parameter of emptyResultSet is false)

  1. Your solution
    Its actually the opposite version of mine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to @hrach no problem :) i kind of prefer the previous version too. I was actually quite thinking about the realisation of what you thought

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, now I see. So what about

$old = $this->refCache['referenced'];
parent::emptyResultSet($saveCache);
$this->refCache['referenced'] = $old;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason of adding a parameter was that i was thinking e.g. about adding new descendant of Selection. Than this behaviour will be probably shared so i choose a parameter.

But i am completely fine with your solution too, if this is not an issue.

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shoud be FALSE



/********************* manipulation ****************d*g**/

Expand Down
6 changes: 4 additions & 2 deletions src/Database/Table/Selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'] = [];
}
}


Expand Down
36 changes: 36 additions & 0 deletions tests/Database/Table/bugs/query.count.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* @dataProvider? ../../databases.ini
*/

use Tester\Assert;

require __DIR__ . '/../../connect.inc.php'; // create $connection

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

// add additional tags (not relevant to other tests)
$context->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);


});