Skip to content

Commit

Permalink
Fix traversal when parent model is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Apr 19, 2021
1 parent 1196e89 commit f519577
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/Persistence/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,15 @@ protected function setLimitOrder(Model $model, \Atk4\Data\Action\Iterator $actio
*/
public function applyScope(Model $model, \Atk4\Data\Action\Iterator $iterator)
{
return $iterator->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());
}

return $iterator->filter($scope);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Persistence/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@ 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));
$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 f519577

Please sign in to comment.