Skip to content

Commit

Permalink
Fixed retrieval of belongs-to relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Mar 4, 2017
1 parent ad2a81d commit 99d1e84
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ FIXED:

- `WHERE` conditions won't be erased when using `SelectQueryBuilder::fetchById()`
- Fetching relationships of an empty list of entities won't raise syntax error
- Retrieving belongs-to relationships won't raise exception
- Retrieval of belongs-to relationships

## 0.2.0 - 2017-02-17

Expand Down
14 changes: 8 additions & 6 deletions src/Model/Relationship/AbstractRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ public function cascadeDelete(Persister $persister, string $relationshipName, $p
}
}

protected function getWhereCondition(string $prefix, string $foreignKey, array $entities): ConditionBuilderInterface
{
protected function getWhereCondition(
array $entities,
string $entityKey,
string $prefix,
string $foreignKey
): ConditionBuilderInterface {
$values = [];
foreach ($entities as $entity) {
$id = $this->parentModel->getId($entity);

if ($id !== null) {
$values[] = $id;
if (isset($entity[$entityKey])) {
$values[] = $entity[$entityKey];
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/Model/Relationship/BelongsToOneRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ public function getQueryBuilder(array $entities): SelectQueryBuilderInterface
$this->relatedModel->getTable()
)
)
->where($this->getWhereCondition($this->relatedModel->getTable(), $this->referencedKey, $entities));
->where(
$this->getWhereCondition(
$entities,
$this->foreignKey,
$this->relatedModel->getTable(),
$this->referencedKey
)
);
}

public function connectToParent(SelectQueryBuilderInterface $selectQueryBuilder)
Expand Down
9 changes: 8 additions & 1 deletion src/Model/Relationship/HasManyThroughRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ public function getQueryBuilder(array $entities): SelectQueryBuilderInterface
$this->relatedModel->getTable()
)
)
->where($this->getWhereCondition($this->junctionModel->getTable(), $this->foreignKey1, $entities));
->where(
$this->getWhereCondition(
$entities,
$this->referencedKey1,
$this->junctionModel->getTable(),
$this->foreignKey1
)
);
}

public function connectToParent(SelectQueryBuilderInterface $selectQueryBuilder)
Expand Down
9 changes: 8 additions & 1 deletion src/Model/Relationship/HasOneRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ public function getQueryBuilder(array $entities): SelectQueryBuilderInterface
return SelectQueryBuilder::create()
->selectColumn("*")
->from($this->relatedModel->getTable())
->where($this->getWhereCondition($this->relatedModel->getTable(), $this->foreignKey, $entities));
->where(
$this->getWhereCondition(
$entities,
$this->referencedKey,
$this->relatedModel->getTable(),
$this->foreignKey
)
);
}

public function connectToParent(SelectQueryBuilderInterface $selectQueryBuilder)
Expand Down

0 comments on commit 99d1e84

Please sign in to comment.