Skip to content
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
18 changes: 12 additions & 6 deletions lib/Doctrine/Dbal/FieldConfigurationSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Rollerworks\Component\Search\Doctrine\Dbal;

use Rollerworks\Component\Search\Doctrine\Dbal\Query\QueryField;
use Rollerworks\Component\Search\Field\OrderField;
use Rollerworks\Component\Search\FieldSet;

/**
Expand All @@ -29,19 +30,24 @@ public function __construct(
) {
}

public function setField(string $fieldName, string $column, ?string $alias = null, string $type = 'string'): void
public function setField(string $mappingName, string $column, ?string $alias = null, string $type = 'string'): void
{
$mappingIdx = null;
$mappingIdx = '';
$fieldName = $mappingName;

if (mb_strpos($fieldName, '#') !== false) {
[$fieldName, $mappingIdx] = explode('#', $fieldName, 2);
if (str_contains($mappingName, '#')) {
[$fieldName, $mappingIdx] = explode('#', $mappingName, 2);
unset($this->fields[$fieldName]['']);
} else {
$this->fields[$fieldName] = [];
$this->fields[$mappingName] = [];
}

if (OrderField::isOrder($fieldName) && str_contains($mappingName, '#')) {
throw new \RuntimeException(\sprintf('Ordering field "%s" cannot be registered with multiple mapping.', $fieldName));
}

$this->fields[$fieldName][$mappingIdx] = new QueryField(
$fieldName . ($mappingIdx !== null ? "#{$mappingIdx}" : ''),
$mappingName,
$this->fieldSet->get($fieldName),
$type,
$column,
Expand Down
7 changes: 6 additions & 1 deletion lib/Doctrine/Orm/FieldConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Rollerworks\Component\Search\Doctrine\Orm;

use Doctrine\ORM\EntityManagerInterface;
use Rollerworks\Component\Search\Field\OrderField;
use Rollerworks\Component\Search\FieldSet;

/**
Expand Down Expand Up @@ -48,13 +49,17 @@ public function setField(string $mappingName, string $property, ?string $alias =
throw new \RuntimeException('No default entity is set, either provide the entity or set a default entity first.');
}

if (mb_strpos($mappingName, '#') !== false) {
if (str_contains($mappingName, '#')) {
[$fieldName, $mappingIdx] = explode('#', $mappingName, 2);
unset($this->fields[$fieldName]['']);
} else {
$this->fields[$fieldName] = [];
}

if (OrderField::isOrder($fieldName) && str_contains($mappingName, '#')) {
throw new \RuntimeException(\sprintf('Ordering field "%s" cannot be registered with multiple mapping.', $fieldName));
}

[$entity, $property] = $this->getEntityAndProperty(
$mappingName,
$entity ?? $this->defaultEntity,
Expand Down
18 changes: 18 additions & 0 deletions lib/Doctrine/Orm/Tests/FieldConfigBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Rollerworks\Component\Search\Doctrine\Orm\OrmQueryField as QueryField;
use Rollerworks\Component\Search\Extension\Core\Type\IntegerType;
use Rollerworks\Component\Search\Extension\Core\Type\TextType;
use Rollerworks\Component\Search\Field\OrderFieldType;
use Rollerworks\Component\Search\Searches;
use Rollerworks\Component\Search\SearchFactory;
use Rollerworks\Component\Search\Tests\Doctrine\Orm\Fixtures\Entity\ECommerceCustomer;
Expand Down Expand Up @@ -212,6 +213,23 @@ public function fails_when_no_entity_is_provided(): void
$fieldConfigBuilder->setField('id', 'id');
}

/**
* @test
*/
public function fails_when_ordering_with_multi_mapping(): void
{
$this->getFieldSet(false)
->add('@id', OrderFieldType::class)
->getFieldSet('invoice');

$fieldConfigBuilder = new FieldConfigBuilder($this->em->reveal(), $this->getFieldSet());

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Ordering field "@id" cannot be registered with multiple mapping.');

$fieldConfigBuilder->setField('@id#1', 'id', 'I', 'Invoice');
}

/**
* @test
*/
Expand Down
Loading