diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cf6956..865bfb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Model/Relationship/AbstractRelationship.php b/src/Model/Relationship/AbstractRelationship.php index 68ee59c..b9a86f0 100644 --- a/src/Model/Relationship/AbstractRelationship.php +++ b/src/Model/Relationship/AbstractRelationship.php @@ -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]; } } diff --git a/src/Model/Relationship/BelongsToOneRelationship.php b/src/Model/Relationship/BelongsToOneRelationship.php index a42418a..2822694 100644 --- a/src/Model/Relationship/BelongsToOneRelationship.php +++ b/src/Model/Relationship/BelongsToOneRelationship.php @@ -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) diff --git a/src/Model/Relationship/HasManyThroughRelationship.php b/src/Model/Relationship/HasManyThroughRelationship.php index 00faf14..90be44a 100644 --- a/src/Model/Relationship/HasManyThroughRelationship.php +++ b/src/Model/Relationship/HasManyThroughRelationship.php @@ -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) diff --git a/src/Model/Relationship/HasOneRelationship.php b/src/Model/Relationship/HasOneRelationship.php index 3a60a5a..70353fb 100644 --- a/src/Model/Relationship/HasOneRelationship.php +++ b/src/Model/Relationship/HasOneRelationship.php @@ -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)