Skip to content

Commit

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

/**
Expand Down
19 changes: 4 additions & 15 deletions src/Persistence/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,23 +364,12 @@ protected function setLimitOrder(Model $model, Query $query)
}

/**
* Will apply $model->scope() conditions onto $query.
* Will apply a condition defined inside $condition or $model->scope() onto $query.
*/
public function initQueryConditions(Model $model, Query $query): void
public function initQueryConditions(Model $model, Query $query, Model\Scope\AbstractScope $condition = null): void
{
$scope = $model->scope();
$condition = $condition ?? $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());
}

$this->_initQueryConditions($query, $scope);
}

private function _initQueryConditions(Query $query, Model\Scope\AbstractScope $condition = null): void
{
if (!$condition->isEmpty()) {
// peel off the single nested scopes to convert (((field = value))) to field = value
$condition = $condition->simplify();
Expand All @@ -395,7 +384,7 @@ private function _initQueryConditions(Query $query, Model\Scope\AbstractScope $c
$expression = $condition->isOr() ? $query->orExpr() : $query->andExpr();

foreach ($condition->getNestedConditions() as $nestedCondition) {
$this->_initQueryConditions($expression, $nestedCondition);
$this->initQueryConditions($model, $expression, $nestedCondition);
}

$query->where($expression);
Expand Down
17 changes: 7 additions & 10 deletions tests/ConditionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Atk4\Data\Model;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Platforms\SqlitePlatform;

class ConditionSqlTest extends \Atk4\Schema\PhpunitTestCase
{
Expand All @@ -29,21 +30,17 @@ public function testBasic()

$mm = clone $m;
$mm->addCondition('gender', 'M');

$this->assertSameSql(
'select "id","name","gender" from "user" where "gender" = :a',
$mm->action('select')->render()
);

$mm->tryLoad(1);
$this->assertSame('John', $mm->get('name'));
$mm->tryLoad(2);
$this->assertNull($mm->get('name'));

$this->assertSameSql(
'select "id","name","gender" from "user" where ("gender" = :a and "id" = :b)',
$mm->action('select')->render()
);
if ($this->getDatabasePlatform() instanceof SqlitePlatform) {
$this->assertSame(
'select "id","name","gender" from "user" where "gender" = :a',
$mm->action('select')->render()
);
}

$mm = clone $m;
$mm->withId(2); // = addCondition(id, 2)
Expand Down
2 changes: 1 addition & 1 deletion tests/RandomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public function testGetTitle()

$m->load(2);
$this->assertSame('Sue', $m->getTitle()); // loaded returns title_field value
$this->assertSame([2 => 'Sue'], $m->getTitles()); // traverse loaded
$this->assertSame([1 => 'John', 2 => 'Sue'], $m->getTitles()); // all titles

// set custom title_field
$m->title_field = 'parent_item_id';
Expand Down

0 comments on commit b62886d

Please sign in to comment.