Skip to content

Commit

Permalink
Fix traversal when parent model is loaded (#853)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Apr 24, 2021
1 parent 9b69ce0 commit 184421a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ jobs:
if: matrix.type == 'Phpunit'
run: "vendor/bin/phpunit \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\" -v"

- name: "Run tests: SQLite Hintable (only for Phpunit)"
if: matrix.type == 'Phpunit'
run: |
sed -i 's~"psr-4": {~"psr-4": { "Mvorisek\\\\Atk4\\\\Hintable\\\\Tests\\\\": "vendor/mahalux/atk4-hintable/tests/",~' composer.json && composer dump
vendor/bin/phpunit --configuration vendor/mahalux/atk4-hintable/phpunit.xml.dist --bootstrap vendor/autoload.php --no-coverage -v
- name: Check Coding Style (only for CodingStyle)
if: matrix.type == 'CodingStyle'
run: vendor/bin/php-cs-fixer fix --dry-run --using-cache=no --diff --diff-format=udiff --verbose --show-progress=dots
Expand Down
10 changes: 9 additions & 1 deletion src/Persistence/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,15 @@ protected function setLimitOrder(Model $model, Action $action)
*/
protected function applyScope(Model $model, Action $action): void
{
$action->filter($model->scope());
$scope = $model->scope();

// add entity ID to scope to allow easy traversal
if ($model->id_field && $model->getId() !== null) {
$scope = new Model\Scope([$scope]);
$scope->addCondition($model->getField($model->id_field), $model->getId());
}

$action->filter($scope);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Persistence/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Atk4\Dsql\Expression;
use Atk4\Dsql\Query;
use Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\SQLServer2012Platform;

class Sql extends Persistence
{
Expand Down Expand Up @@ -369,6 +371,18 @@ protected function setLimitOrder(Model $model, Query $query)
public function initQueryConditions(Model $model, Query $query): void
{
$this->_initQueryConditions($query, $model->scope());

// add entity ID to scope to allow easy traversal
if ($model->id_field && $model->getId() !== null) {
$query->group($model->getField($model->id_field));
if ($this->getDatabasePlatform() instanceof SQLServer2012Platform
|| $this->getDatabasePlatform() instanceof OraclePlatform) {
foreach ($query->args['field'] as $alias => $field) {
$query->group(is_int($alias) ? $field : $alias);
}
}
$query->having($model->getField($model->id_field), $model->getId());
}
}

private function _initQueryConditions(Query $query, Model\Scope\AbstractScope $condition = null): void
Expand Down
6 changes: 3 additions & 3 deletions tests/ExpressionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ public function testQuery()
);
}

$i->tryLoad(1);
$this->assertEquals(10, $i->get('total_net'));
$this->assertEquals(30, $i->get('sum_net'));
$ii = (clone $i)->tryLoad(1);
$this->assertEquals(10, $ii->get('total_net'));
$this->assertEquals(30, $ii->get('sum_net'));

$q = $db->dsql();
$q->field($i->action('count'), 'total_orders');
Expand Down

0 comments on commit 184421a

Please sign in to comment.