Skip to content

Commit

Permalink
fix bug in match function
Browse files Browse the repository at this point in the history
  • Loading branch information
znck committed Jun 15, 2015
1 parent cbb5009 commit 772ef31
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/BelongsToThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class BelongsToThrough extends Relation
* @param string $firstKey
* @param string $localKey
*/
public function __construct(Builder $query, Model $farChild, Model $parent, $firstKey, $localKey)
function __construct(Builder $query, Model $farChild, Model $parent, $firstKey, $localKey)
{
$this->farChild = $farChild;
$this->firstKey = $firstKey;
Expand All @@ -63,6 +63,8 @@ public function addConstraints()

$this->setJoin();

$this->query->addSelect([$this->related->getTable() . '.*']);

if (static::$constraints) {
$this->query->where($parentTable . '.' . $this->parent->getKeyName(), '=', $localValue);
}
Expand Down Expand Up @@ -111,7 +113,9 @@ public function addEagerConstraints(array $models)
{
$table = $this->parent->getTable();

$this->query->whereIn($table . '.' . $this->parent->getKeyName(), $this->getKeys($models));
$this->query->addSelect([$this->parent->getTable() . '.' . $this->parent->getKeyName() .' as __related_through_key']);

$this->query->whereIn($table . '.' . $this->parent->getKeyName(), $this->getKeys($models, $this->localKey));
}

/**
Expand All @@ -125,7 +129,7 @@ public function addEagerConstraints(array $models)
public function initRelation(array $models, $relation)
{
foreach ($models as $model) {
$model->setRelation($relation, $this->related->newCollection());
$model->setRelation($relation, $this->related);
}

return $models;
Expand All @@ -148,7 +152,7 @@ public function match(array $models, Collection $results, $relation)
// link them up with their parent using the keyed dictionary to make the
// matching very convenient and easy work. Then we'll just return them.
foreach ($models as $model) {
$key = $model->getKey();
$key = $model->{$this->localKey};

if (isset($dictionary[$key])) {
$value = $dictionary[$key];
Expand All @@ -171,13 +175,14 @@ protected function buildDictionary(Collection $results)
{
$dictionary = [];

$foreign = $this->firstKey;
$foreign = '__related_through_key';

// First we will create a dictionary of models keyed by the foreign key of the
// relationship as this will allow us to quickly access all of the related
// models without having to do nested looping which will be quite slow.
foreach ($results as $result) {
$dictionary[$result->{$foreign}] = $result;
$result->offsetUnset($foreign);
}

return $dictionary;
Expand All @@ -190,6 +195,11 @@ protected function buildDictionary(Collection $results)
*/
public function getResults()
{
return $this->query->getModel();
return $this->query->first();
}

public function get()
{
return $this->query->get();
}
}
}

0 comments on commit 772ef31

Please sign in to comment.