Skip to content

Commit

Permalink
collection: implemented mapping & modifiers support [closes #117]
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Oct 10, 2015
1 parent ca9ef7b commit 2b7b0c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Mapper/Dbal/QueryBuilderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ public function processWhereExpression($expression, $value, QueryBuilder $builde
return;
}

$sqlExpresssion = $this->normalizeAndAddJoins($chain, $sourceEntity, $builder, $distinctNeeded, $value);
$modifier = '%any';
$sqlExpresssion = $this->normalizeAndAddJoins($chain, $sourceEntity, $builder, $distinctNeeded, $value, $modifier);
$operator = $this->getSqlOperator($value, $operator);

$builder->andWhere($sqlExpresssion . $operator . '%any', $value);
$builder->andWhere($sqlExpresssion . $operator . $modifier, $value);
}


Expand All @@ -91,7 +92,7 @@ public function processOrderByExpression($expression, $direction, QueryBuilder $
}


private function normalizeAndAddJoins(array $levels, $sourceEntity, QueryBuilder $builder, & $distinctNeeded = FALSE, & $value = NULL)
private function normalizeAndAddJoins(array $levels, $sourceEntity, QueryBuilder $builder, & $distinctNeeded = FALSE, & $value = NULL, & $modifier = '%any')
{
$column = array_pop($levels);
$sourceMapper = $this->mapper;
Expand Down Expand Up @@ -171,7 +172,13 @@ private function normalizeAndAddJoins(array $levels, $sourceEntity, QueryBuilder
return '(' . implode(', ', $pair) . ')';

} else {
$column = $sourceReflection->convertEntityToStorageKey($column);
$converted = $sourceReflection->convertEntityToStorage([$column => $value]);
$column = key($converted);
if (($pos = strpos($column, '%')) !== FALSE) {
$column = substr($column, 0, $pos);
$modifier = substr($column, $pos);
}
$value = current($converted);
return "[{$sourceAlias}.{$column}]";
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/cases/integration/Collection/collection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use NextrasTests\Orm\Book;
use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\TagFollower;
use Tester\Assert;
use Tester\Environment;


$dic = require_once __DIR__ . '/../../../bootstrap.php';

Expand Down Expand Up @@ -144,6 +146,15 @@ class CollectionTest extends DataTestCase
Assert::same(1, $follower->author->id);
}


public function testMappingInCollection()
{
if ($this->section === 'array') Environment::skip('Test is only for Dbal mapper.');

$tags = $this->orm->tags->findBy(['isGlobal' => TRUE]);
Assert::same(2, $tags->countStored());
Assert::same('Tag 1', $tags->fetch()->name);
}
}


Expand Down

0 comments on commit 2b7b0c8

Please sign in to comment.