Skip to content

Commit

Permalink
Merge pull request #143 from homersimpsons/feature/pk-lazy-load
Browse files Browse the repository at this point in the history
Pk Lazy Load
  • Loading branch information
moufmouf committed Jul 11, 2019
2 parents 6cd182e + 1d4ab3c commit 2f5705c
Show file tree
Hide file tree
Showing 27 changed files with 121 additions and 75 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ script:
else
./vendor/bin/phpunit $PHPUNITFILE;
fi
- composer cscheck
- composer phpstan
- composer require-checker
after_script:
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"phpunit/phpunit": "^7.4.4 || ^8.0.0",
"satooshi/php-coveralls": "^1.0.1",
"wa72/simplelogger" : "^1.0",
"friendsofphp/php-cs-fixer": "^2.5",
"friendsofphp/php-cs-fixer": "^2.15.1",
"symfony/process": "^3 || ^4",
"thecodingmachine/tdbm-fluid-schema-builder": "^1.0.0",
"phpstan/phpstan": "^0.11.5",
Expand Down Expand Up @@ -72,7 +72,9 @@
"phpstan": "php -d memory_limit=3G vendor/bin/phpstan analyse src -c phpstan.neon --level=7 --no-progress -vvv",
"require-checker": "composer-require-checker check --config-file=composer-require-checker.json",
"test": "phpunit",
"ci": ["@phpstan", "@test", "@require-checker"],
"csfix": "php-cs-fixer fix src/ && php-cs-fixer fix tests/",
"cscheck": "php-cs-fixer fix src/ --dry-run --stop-on-violation && php-cs-fixer fix tests/ --dry-run --stop-on-violation ",
"ci": ["@cscheck", "@phpstan", "@test", "@require-checker"],
"post-install-cmd": ["@composer bin all install --ansi"],
"post-update-cmd": ["@composer bin all update --ansi"]
},
Expand Down
19 changes: 12 additions & 7 deletions src/DbRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function _attach(TDBMService $tdbmService): void
/**
* Sets the state of the TDBM Object
* One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
* $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
* $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with the "new" keyword.
* $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
* $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
*
Expand Down Expand Up @@ -219,7 +219,9 @@ public function _dbLoadIfNotLoaded(): void
*/
public function get(string $var)
{
$this->_dbLoadIfNotLoaded();
if (!isset($this->primaryKeys[$var])) {
$this->_dbLoadIfNotLoaded();
}

return $this->dbRow[$var] ?? null;
}
Expand Down Expand Up @@ -294,13 +296,16 @@ public function getRef(string $foreignKeyName) : ?AbstractTDBMObject
$values[] = $this->dbRow[$column];
}

$filter = SafeFunctions::arrayCombine($fk->getUnquotedForeignColumns(), $values);
$foreignColumns = $fk->getUnquotedForeignColumns();
$foreignTableName = $fk->getForeignTableName();

$filter = SafeFunctions::arrayCombine($foreignColumns, $values);

// If the foreign key points to the primary key, let's use findObjectByPk
if ($this->tdbmService->getPrimaryKeyColumns($fk->getForeignTableName()) === $fk->getUnquotedForeignColumns()) {
return $this->tdbmService->findObjectByPk($fk->getForeignTableName(), $filter, [], true);
if ($this->tdbmService->getPrimaryKeyColumns($foreignTableName) === $foreignColumns) {
return $this->tdbmService->findObjectByPk($foreignTableName, $filter, [], true);
} else {
return $this->tdbmService->findObject($fk->getForeignTableName(), $filter);
return $this->tdbmService->findObject($foreignTableName, $filter);
}
}
}
Expand Down Expand Up @@ -445,7 +450,7 @@ public function _getPrimaryKeys(): array

/**
* Sets the values of the primary key.
* This is set when the object is in "loaded" state.
* This is set when the object is in "loaded" or "not loaded" state.
*
* @param mixed[] $primaryKeys
*/
Expand Down
2 changes: 1 addition & 1 deletion src/InnerResultIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function key()
*/
public function next()
{
$row = $this->statement->fetch(\PDO::FETCH_BOTH);
$row = $this->statement->fetch(\PDO::FETCH_ASSOC);
if ($row) {

// array<tablegroup, array<table, array<column, value>>>
Expand Down
2 changes: 1 addition & 1 deletion src/QueryFactory/FindObjectsQueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(string $mainTable, array $additionalTablesFetch, $fi

protected function compute(): void
{
$key = 'FindObjectsQueryFactory_'.$this->mainTable.'__'.implode('_/_',$this->additionalTablesFetch).'__'.$this->filterString.'__'.$this->orderBy;
$key = 'FindObjectsQueryFactory_'.$this->mainTable.'__'.implode('_/_', $this->additionalTablesFetch).'__'.$this->filterString.'__'.$this->orderBy;
if ($this->cache->contains($key)) {
[
$this->magicSql,
Expand Down
3 changes: 1 addition & 2 deletions src/SafeFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace TheCodingMachine\TDBM;


use function array_combine;
use function error_get_last;
use RuntimeException;
Expand All @@ -25,4 +24,4 @@ public static function arrayCombine(array $keys, array $values): array
}
return $array;
}
}
}
28 changes: 18 additions & 10 deletions src/Schema/ForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace TheCodingMachine\TDBM\Schema;


use Doctrine\DBAL\Schema\ForeignKeyConstraint;

class ForeignKey
Expand All @@ -12,17 +11,22 @@ class ForeignKey
public const LOCAL_COLUMNS = 'localColumns';
public const FOREIGN_COLUMNS = 'foreignColumns';

/**
* @var array<string, string|array<string>>
*/
private $foreignKey;
/** @var string */
private $foreignTable;
/** @var string[] */
private $localColumns;
/** @var string[] */
private $foreignColumns;


/**
* @param array<string, string|array<string>> $foreignKey
*/
public function __construct(array $foreignKey)
{
$this->foreignKey = $foreignKey;
$this->foreignTable = $foreignKey[self::FOREIGN_TABLE];
$this->localColumns = $foreignKey[self::LOCAL_COLUMNS];
$this->foreignColumns = $foreignKey[self::FOREIGN_COLUMNS];
}

public static function createFromFk(ForeignKeyConstraint $fk): self
Expand All @@ -39,24 +43,28 @@ public static function createFromFk(ForeignKeyConstraint $fk): self
*/
public function getUnquotedLocalColumns(): array
{
return $this->foreignKey[self::LOCAL_COLUMNS];
return $this->localColumns;
}

/**
* @return array<string>
*/
public function getUnquotedForeignColumns(): array
{
return $this->foreignKey[self::FOREIGN_COLUMNS];
return $this->foreignColumns;
}

public function getForeignTableName(): string
{
return $this->foreignKey[self::FOREIGN_TABLE];
return $this->foreignTable;
}

private $cacheKey;
public function getCacheKey(): string
{
return 'from__'.implode(',', $this->getUnquotedLocalColumns()) . '__to__table__' . $this->getForeignTableName() . '__columns__' . implode(',', $this->getUnquotedForeignColumns());
if ($this->cacheKey === null) {
$this->cacheKey = 'from__' . implode(',', $this->getUnquotedLocalColumns()) . '__to__table__' . $this->getForeignTableName() . '__columns__' . implode(',', $this->getUnquotedForeignColumns());
}
return $this->cacheKey;
}
}
2 changes: 0 additions & 2 deletions src/Schema/ForeignKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace TheCodingMachine\TDBM\Schema;


class ForeignKeys
{
/**
Expand Down Expand Up @@ -32,5 +31,4 @@ public function getForeignKey(string $fkName): ForeignKey
}
return $this->foreignKey[$fkName];
}

}
4 changes: 3 additions & 1 deletion src/Utils/Annotation/AnnotationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ private function parse(string $comment, string $context): Annotations

// Let's add * in front of the line (otherwise, parsing is failing)
$lines = explode("\n", $comment);
$lines = array_map(function(string $line) { return '* '.$line; }, $lines);
$lines = array_map(function (string $line) {
return '* '.$line;
}, $lines);
$comment = implode("\n", $lines);

$annotations = $this->docParser->parse($comment, $context);
Expand Down
1 change: 0 additions & 1 deletion src/Utils/BaseCodeGeneratorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
class BaseCodeGeneratorListener implements CodeGeneratorListenerInterface
{

public function onBaseBeanGenerated(FileGenerator $fileGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration): ?FileGenerator
{
return $fileGenerator;
Expand Down
Loading

0 comments on commit 2f5705c

Please sign in to comment.