Skip to content

Commit 2f5705c

Browse files
authored
Merge pull request #143 from homersimpsons/feature/pk-lazy-load
Pk Lazy Load
2 parents 6cd182e + 1d4ab3c commit 2f5705c

27 files changed

+121
-75
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ script:
7777
else
7878
./vendor/bin/phpunit $PHPUNITFILE;
7979
fi
80+
- composer cscheck
8081
- composer phpstan
8182
- composer require-checker
8283
after_script:

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"phpunit/phpunit": "^7.4.4 || ^8.0.0",
4545
"satooshi/php-coveralls": "^1.0.1",
4646
"wa72/simplelogger" : "^1.0",
47-
"friendsofphp/php-cs-fixer": "^2.5",
47+
"friendsofphp/php-cs-fixer": "^2.15.1",
4848
"symfony/process": "^3 || ^4",
4949
"thecodingmachine/tdbm-fluid-schema-builder": "^1.0.0",
5050
"phpstan/phpstan": "^0.11.5",
@@ -72,7 +72,9 @@
7272
"phpstan": "php -d memory_limit=3G vendor/bin/phpstan analyse src -c phpstan.neon --level=7 --no-progress -vvv",
7373
"require-checker": "composer-require-checker check --config-file=composer-require-checker.json",
7474
"test": "phpunit",
75-
"ci": ["@phpstan", "@test", "@require-checker"],
75+
"csfix": "php-cs-fixer fix src/ && php-cs-fixer fix tests/",
76+
"cscheck": "php-cs-fixer fix src/ --dry-run --stop-on-violation && php-cs-fixer fix tests/ --dry-run --stop-on-violation ",
77+
"ci": ["@cscheck", "@phpstan", "@test", "@require-checker"],
7678
"post-install-cmd": ["@composer bin all install --ansi"],
7779
"post-update-cmd": ["@composer bin all update --ansi"]
7880
},

src/DbRow.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function _attach(TDBMService $tdbmService): void
159159
/**
160160
* Sets the state of the TDBM Object
161161
* One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
162-
* $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
162+
* $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with the "new" keyword.
163163
* $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
164164
* $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
165165
*
@@ -219,7 +219,9 @@ public function _dbLoadIfNotLoaded(): void
219219
*/
220220
public function get(string $var)
221221
{
222-
$this->_dbLoadIfNotLoaded();
222+
if (!isset($this->primaryKeys[$var])) {
223+
$this->_dbLoadIfNotLoaded();
224+
}
223225

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

297-
$filter = SafeFunctions::arrayCombine($fk->getUnquotedForeignColumns(), $values);
299+
$foreignColumns = $fk->getUnquotedForeignColumns();
300+
$foreignTableName = $fk->getForeignTableName();
301+
302+
$filter = SafeFunctions::arrayCombine($foreignColumns, $values);
298303

299304
// If the foreign key points to the primary key, let's use findObjectByPk
300-
if ($this->tdbmService->getPrimaryKeyColumns($fk->getForeignTableName()) === $fk->getUnquotedForeignColumns()) {
301-
return $this->tdbmService->findObjectByPk($fk->getForeignTableName(), $filter, [], true);
305+
if ($this->tdbmService->getPrimaryKeyColumns($foreignTableName) === $foreignColumns) {
306+
return $this->tdbmService->findObjectByPk($foreignTableName, $filter, [], true);
302307
} else {
303-
return $this->tdbmService->findObject($fk->getForeignTableName(), $filter);
308+
return $this->tdbmService->findObject($foreignTableName, $filter);
304309
}
305310
}
306311
}
@@ -445,7 +450,7 @@ public function _getPrimaryKeys(): array
445450

446451
/**
447452
* Sets the values of the primary key.
448-
* This is set when the object is in "loaded" state.
453+
* This is set when the object is in "loaded" or "not loaded" state.
449454
*
450455
* @param mixed[] $primaryKeys
451456
*/

src/InnerResultIterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public function key()
177177
*/
178178
public function next()
179179
{
180-
$row = $this->statement->fetch(\PDO::FETCH_BOTH);
180+
$row = $this->statement->fetch(\PDO::FETCH_ASSOC);
181181
if ($row) {
182182

183183
// array<tablegroup, array<table, array<column, value>>>

src/QueryFactory/FindObjectsQueryFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(string $mainTable, array $additionalTablesFetch, $fi
3333

3434
protected function compute(): void
3535
{
36-
$key = 'FindObjectsQueryFactory_'.$this->mainTable.'__'.implode('_/_',$this->additionalTablesFetch).'__'.$this->filterString.'__'.$this->orderBy;
36+
$key = 'FindObjectsQueryFactory_'.$this->mainTable.'__'.implode('_/_', $this->additionalTablesFetch).'__'.$this->filterString.'__'.$this->orderBy;
3737
if ($this->cache->contains($key)) {
3838
[
3939
$this->magicSql,

src/SafeFunctions.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace TheCodingMachine\TDBM;
55

6-
76
use function array_combine;
87
use function error_get_last;
98
use RuntimeException;
@@ -25,4 +24,4 @@ public static function arrayCombine(array $keys, array $values): array
2524
}
2625
return $array;
2726
}
28-
}
27+
}

src/Schema/ForeignKey.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace TheCodingMachine\TDBM\Schema;
55

6-
76
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
87

98
class ForeignKey
@@ -12,17 +11,22 @@ class ForeignKey
1211
public const LOCAL_COLUMNS = 'localColumns';
1312
public const FOREIGN_COLUMNS = 'foreignColumns';
1413

15-
/**
16-
* @var array<string, string|array<string>>
17-
*/
18-
private $foreignKey;
14+
/** @var string */
15+
private $foreignTable;
16+
/** @var string[] */
17+
private $localColumns;
18+
/** @var string[] */
19+
private $foreignColumns;
20+
1921

2022
/**
2123
* @param array<string, string|array<string>> $foreignKey
2224
*/
2325
public function __construct(array $foreignKey)
2426
{
25-
$this->foreignKey = $foreignKey;
27+
$this->foreignTable = $foreignKey[self::FOREIGN_TABLE];
28+
$this->localColumns = $foreignKey[self::LOCAL_COLUMNS];
29+
$this->foreignColumns = $foreignKey[self::FOREIGN_COLUMNS];
2630
}
2731

2832
public static function createFromFk(ForeignKeyConstraint $fk): self
@@ -39,24 +43,28 @@ public static function createFromFk(ForeignKeyConstraint $fk): self
3943
*/
4044
public function getUnquotedLocalColumns(): array
4145
{
42-
return $this->foreignKey[self::LOCAL_COLUMNS];
46+
return $this->localColumns;
4347
}
4448

4549
/**
4650
* @return array<string>
4751
*/
4852
public function getUnquotedForeignColumns(): array
4953
{
50-
return $this->foreignKey[self::FOREIGN_COLUMNS];
54+
return $this->foreignColumns;
5155
}
5256

5357
public function getForeignTableName(): string
5458
{
55-
return $this->foreignKey[self::FOREIGN_TABLE];
59+
return $this->foreignTable;
5660
}
5761

62+
private $cacheKey;
5863
public function getCacheKey(): string
5964
{
60-
return 'from__'.implode(',', $this->getUnquotedLocalColumns()) . '__to__table__' . $this->getForeignTableName() . '__columns__' . implode(',', $this->getUnquotedForeignColumns());
65+
if ($this->cacheKey === null) {
66+
$this->cacheKey = 'from__' . implode(',', $this->getUnquotedLocalColumns()) . '__to__table__' . $this->getForeignTableName() . '__columns__' . implode(',', $this->getUnquotedForeignColumns());
67+
}
68+
return $this->cacheKey;
6169
}
6270
}

src/Schema/ForeignKeys.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace TheCodingMachine\TDBM\Schema;
55

6-
76
class ForeignKeys
87
{
98
/**
@@ -32,5 +31,4 @@ public function getForeignKey(string $fkName): ForeignKey
3231
}
3332
return $this->foreignKey[$fkName];
3433
}
35-
3634
}

src/Utils/Annotation/AnnotationParser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ private function parse(string $comment, string $context): Annotations
7070

7171
// Let's add * in front of the line (otherwise, parsing is failing)
7272
$lines = explode("\n", $comment);
73-
$lines = array_map(function(string $line) { return '* '.$line; }, $lines);
73+
$lines = array_map(function (string $line) {
74+
return '* '.$line;
75+
}, $lines);
7476
$comment = implode("\n", $lines);
7577

7678
$annotations = $this->docParser->parse($comment, $context);

src/Utils/BaseCodeGeneratorListener.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
class BaseCodeGeneratorListener implements CodeGeneratorListenerInterface
1818
{
19-
2019
public function onBaseBeanGenerated(FileGenerator $fileGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration): ?FileGenerator
2120
{
2221
return $fileGenerator;

0 commit comments

Comments
 (0)