Skip to content
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

Fix traversal when parent model is loaded #853

Merged
merged 2 commits into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
DarkSide666 marked this conversation as resolved.
Show resolved Hide resolved
{
$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());
Copy link
Member Author

@mvorisek mvorisek Apr 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: having with group by is like where, but it does not apply after aggregate functions.

}
}

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