Skip to content

Commit

Permalink
relationships: fixed query builder for 1:1d filtering [closes #98]
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Aug 18, 2015
1 parent 3e580f3 commit 3846fd8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Mapper/Dbal/QueryBuilderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Nextras\Orm\Collection\Helpers\ConditionParserHelper;
use Nextras\Orm\Collection\ICollection;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Entity\Reflection\PropertyRelationshipMetadata;
use Nextras\Orm\Entity\Reflection\PropertyRelationshipMetadata as Relationship;
use Nextras\Orm\LogicException;
use Nextras\Orm\Model\IModel;
use Nextras\Orm\Model\MetadataStorage;
Expand Down Expand Up @@ -109,12 +109,13 @@ private function normalizeAndAddJoins(array $levels, $sourceEntity, QueryBuilder
$targetReflection = $targetMapper->getStorageReflection();
$targetEntityMetadata = $this->metadataStorage->get($property->relationship->entity);

if ($property->relationship->type === PropertyRelationshipMetadata::ONE_HAS_MANY) {
$relType = $property->relationship->type;
if ($relType === Relationship::ONE_HAS_MANY || ($relType === Relationship::ONE_HAS_ONE_DIRECTED && !$property->relationship->isMain)) {
$targetColumn = $targetReflection->convertEntityToStorageKey($property->relationship->property);
$sourceColumn = $sourceReflection->getStoragePrimaryKey()[0];
$distinctNeeded = TRUE;

} elseif ($property->relationship->type === PropertyRelationshipMetadata::MANY_HAS_MANY) {
} elseif ($relType === Relationship::MANY_HAS_MANY) {
if ($property->relationship->isMain) {
list($joinTable, list($inColumn, $outColumn)) = $sourceMapper->getManyHasManyParameters($property, $targetMapper);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,29 @@ class RelationshipOneHasOneDTest extends DataTestCase
}


public function testQueryBuilder()
{
$book = new Book();
$book->author = $this->orm->authors->getById(1);
$book->title = 'Games of Thrones I';
$book->publisher = 1;

$ean = new Ean();
$ean->code = '1234';
$ean->book = $book;

$this->orm->books->persistAndFlush($book);

$books = $this->orm->books->findBy(['this->ean->code' => '1234']);
Assert::same(1, $books->countStored());
Assert::same(1, $books->count());

$eans = $this->orm->eans->findBy(['this->book->title' => 'Games of Thrones I']);
Assert::same(1, $eans->countStored());
Assert::same(1, $eans->count());
}


public function testRemove()
{
/** @var Author $author */
Expand Down

0 comments on commit 3846fd8

Please sign in to comment.