Skip to content

Smart eager loading #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Generating partial query only once
  • Loading branch information
moufmouf committed Sep 9, 2019
commit a25a8f6bcf4bc4c6a1ce619bf03e0b4d43b6fae7
24 changes: 13 additions & 11 deletions src/InnerResultIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\PartialQueryFactory;
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\Query\PartialQuery;
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\StorageNode;
use TheCodingMachine\TDBM\Utils\DbalUtils;

Expand Down Expand Up @@ -68,11 +69,13 @@ class InnerResultIterator implements \Iterator, \Countable, \ArrayAccess
*/
private $logger;
/**
* @var PartialQueryFactory|null
* @var PartialQuery|null
*/
private $partialQueryFactory;

protected $count = null;
private $partialQuery;
/**
* @var int|null
*/
protected $count;

private function __construct()
{
Expand All @@ -96,7 +99,11 @@ public static function createInnerResultIterator(string $magicSql, array $parame
$iterator->magicQuery = $magicQuery;
$iterator->databasePlatform = $iterator->tdbmService->getConnection()->getDatabasePlatform();
$iterator->logger = $logger;
$iterator->partialQueryFactory = $partialQueryFactory;
$partialQuery = null;
if ($iterator instanceof StorageNode && $partialQueryFactory !== null) {
$iterator->partialQuery = $partialQueryFactory->getPartialQuery($iterator, $magicQuery, $parameters);
}

return $iterator;
}

Expand Down Expand Up @@ -208,11 +215,6 @@ public function next()
$beansData[$columnDescriptor['tableGroup']][$columnDescriptor['table']][$columnDescriptor['column']] = $value;
}

$partialQuery = null;
if ($this instanceof StorageNode && $this->partialQueryFactory !== null) {
$partialQuery = $this->partialQueryFactory->getPartialQuery($this, $this->magicQuery, $this->parameters);
}

$reflectionClassCache = [];
$firstBean = true;
foreach ($beansData as $beanData) {
Expand Down Expand Up @@ -250,7 +252,7 @@ public function next()
// Let's bypass the constructor when creating the bean!
/** @var AbstractTDBMObject $bean */
$bean = $reflectionClassCache[$actualClassName]->newInstanceWithoutConstructor();
$bean->_constructFromData($beanData, $this->tdbmService, $partialQuery);
$bean->_constructFromData($beanData, $this->tdbmService, $this->partialQuery);
}

// The first bean is the one containing the main table.
Expand Down