Skip to content

Commit

Permalink
Merge pull request #193 from peter-gribanov/index_by_field
Browse files Browse the repository at this point in the history
Allow use a Field operand in IndexBy spec
  • Loading branch information
peter-gribanov committed May 7, 2019
2 parents 83b98a8 + a4e4dc2 commit 501849a
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Query/IndexBy.php
Expand Up @@ -3,16 +3,17 @@
namespace Happyr\DoctrineSpecification\Query;

use Doctrine\ORM\QueryBuilder;
use Happyr\DoctrineSpecification\Operand\Field;

/**
* Class IndexBy.
*/
class IndexBy implements QueryModifier
{
/**
* Field name.
* Field.
*
* @var string
* @var Field
*/
private $field;

Expand All @@ -26,11 +27,14 @@ class IndexBy implements QueryModifier
/**
* IndexBy constructor.
*
* @param string $field Field name for indexing
* @param string $dqlAlias DQL alias of field
* @param Field|string $field Field name for indexing
* @param string $dqlAlias DQL alias of field
*/
public function __construct($field, $dqlAlias = null)
{
if (!($field instanceof Field)) {
$field = new Field($field);
}
$this->field = $field;
$this->dqlAlias = $dqlAlias;
}
Expand All @@ -44,6 +48,6 @@ public function modify(QueryBuilder $qb, $dqlAlias)
$dqlAlias = $this->dqlAlias;
}

$qb->indexBy($dqlAlias, sprintf('%s.%s', $dqlAlias, $this->field));
$qb->indexBy($dqlAlias, $this->field->transform($qb, $dqlAlias));
}
}

0 comments on commit 501849a

Please sign in to comment.