-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring retrieveManyToOneRelationshipsStorage signature
This will allow us in the future to build a oneToMany handler for smart eager loading.
- Loading branch information
Showing
7 changed files
with
166 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
|
||
namespace TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use TheCodingMachine\TDBM\TDBMException; | ||
|
||
class OneToManyDataLoader | ||
{ | ||
/** | ||
* @var Connection | ||
*/ | ||
private $connection; | ||
/** | ||
* @var string | ||
*/ | ||
private $sql; | ||
/** | ||
* @var string | ||
*/ | ||
private $fkColumn; | ||
/** | ||
* @var array<string, array<int, array<string, mixed>>> Array of rows, indexed by foreign key. | ||
*/ | ||
private $data; | ||
|
||
public function __construct(Connection $connection, string $sql, string $fkColumn) | ||
{ | ||
$this->connection = $connection; | ||
$this->sql = $sql; | ||
$this->fkColumn = $fkColumn; | ||
} | ||
|
||
/** | ||
* @return array<string, array<string, mixed>> Rows, indexed by ID. | ||
*/ | ||
private function load(): array | ||
{ | ||
$results = $this->connection->fetchAll($this->sql); | ||
|
||
$data = []; | ||
foreach ($results as $row) { | ||
$data[$row[$this->fkColumn]][] = $row; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Returns the DB row with the given ID. | ||
* Loads all rows if necessary. | ||
* Throws an exception if nothing found. | ||
* | ||
* @param string $id | ||
* @return array<string, mixed> | ||
*/ | ||
public function get(string $id): array | ||
{ | ||
if ($this->data === null) { | ||
$this->data = $this->load(); | ||
} | ||
|
||
if (!isset($this->data[$id])) { | ||
return []; | ||
} | ||
return $this->data[$id]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
|
||
namespace TheCodingMachine\TDBM\Utils; | ||
|
||
use function array_keys; | ||
use function count; | ||
use function implode; | ||
use function is_array; | ||
use function range; | ||
use function var_export; | ||
|
||
class Psr2Utils | ||
{ | ||
/** | ||
* @param mixed $var | ||
* @param string $indent | ||
* @return string | ||
*/ | ||
public static function psr2VarExport($var, string $indent=''): string | ||
{ | ||
if (is_array($var)) { | ||
$indexed = array_keys($var) === range(0, count($var) - 1); | ||
$r = []; | ||
foreach ($var as $key => $value) { | ||
$r[] = "$indent " | ||
. ($indexed ? '' : self::psr2VarExport($key) . ' => ') | ||
. self::psr2VarExport($value, "$indent "); | ||
} | ||
return "[\n" . implode(",\n", $r) . "\n" . $indent . ']'; | ||
} | ||
return var_export($var, true); | ||
} | ||
|
||
/** | ||
* @param mixed $var | ||
* @return string | ||
*/ | ||
public static function psr2InlineVarExport($var): string | ||
{ | ||
if (is_array($var)) { | ||
$indexed = array_keys($var) === range(0, count($var) - 1); | ||
$r = []; | ||
foreach ($var as $key => $value) { | ||
$r[] = ($indexed ? '' : self::psr2InlineVarExport($key) . ' => ') | ||
. self::psr2InlineVarExport($value); | ||
} | ||
return '[' . implode(',', $r) . ']'; | ||
} | ||
return var_export($var, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters