Fix Eloquent eager loading matching.
This commit is contained in:
@@ -87,9 +87,14 @@ class Belongs_To extends Relationship {
|
||||
|
||||
foreach ($children as &$child)
|
||||
{
|
||||
if (array_key_exists($child->$foreign, $parents))
|
||||
$parent = array_first($parents, function($k, $v) use ($child, $foreign)
|
||||
{
|
||||
$child->relationships[$relationship] = $parents[$child->$foreign];
|
||||
return $v->get_key() == $child->$foreign;
|
||||
});
|
||||
|
||||
if ( ! is_null($parent))
|
||||
{
|
||||
$child->relationships[$relationship] = $parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,9 +91,14 @@ class Has_Many extends Has_One_Or_Many {
|
||||
{
|
||||
$foreign = $this->foreign_key();
|
||||
|
||||
foreach ($children as $key => $child)
|
||||
foreach ($parents as &$parent)
|
||||
{
|
||||
$parents[$child->$foreign]->relationships[$relationship][$child->get_key()] = $child;
|
||||
$matching = array_filter($children, function($v) use ($parent, $foreign)
|
||||
{
|
||||
return $v->$foreign == $parent->get_key();
|
||||
});
|
||||
|
||||
$parent->relationships[$relationship] = $matching;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -311,7 +311,7 @@ class Has_Many_And_Belongs_To extends Relationship {
|
||||
*/
|
||||
public function eagerly_constrain($results)
|
||||
{
|
||||
$this->table->where_in($this->joining.'.'.$this->foreign_key(), array_keys($results));
|
||||
$this->table->where_in($this->joining.'.'.$this->foreign_key(), $this->keys($results));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -325,14 +325,14 @@ class Has_Many_And_Belongs_To extends Relationship {
|
||||
{
|
||||
$foreign = $this->foreign_key();
|
||||
|
||||
// For each child we'll just get the parent that connects to the child and set the
|
||||
// child model on the relationship array using the keys. Once we're done looping
|
||||
// through the children all of the proper relations will be set.
|
||||
foreach ($children as $key => $child)
|
||||
foreach ($parents as &$parent)
|
||||
{
|
||||
$parent =& $parents[$child->pivot->$foreign];
|
||||
$matching = array_filter($children, function($v) use ($parent, $foreign)
|
||||
{
|
||||
return $v->pivot->$foreign == $parent->get_key();
|
||||
});
|
||||
|
||||
$parent->relationships[$relationship][$child->{$child->key()}] = $child;
|
||||
$parent->relationships[$relationship] = $matching;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,9 +38,14 @@ class Has_One extends Has_One_Or_Many {
|
||||
{
|
||||
$foreign = $this->foreign_key();
|
||||
|
||||
foreach ($children as $key => $child)
|
||||
foreach ($parents as &$parent)
|
||||
{
|
||||
$parents[$child->$foreign]->relationships[$relationship] = $child;
|
||||
$matching = array_first($children, function($k, $v) use ($parent, $foreign)
|
||||
{
|
||||
return $v->$foreign == $parent->get_key();
|
||||
});
|
||||
|
||||
$parent->relationships[$relationship] = $matching;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ class Has_One_Or_Many extends Relationship {
|
||||
*/
|
||||
public function eagerly_constrain($results)
|
||||
{
|
||||
$this->table->where_in($this->foreign_key(), array_keys($results));
|
||||
$this->table->where_in($this->foreign_key(), $this->keys($results));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -101,4 +101,22 @@ abstract class Relationship extends Query {
|
||||
return static::foreign($this->base, $this->foreign);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gather all the primary keys from a result set.
|
||||
*
|
||||
* @param array $results
|
||||
* @return array
|
||||
*/
|
||||
public function keys($results)
|
||||
{
|
||||
$keys = array();
|
||||
|
||||
foreach ($results as $result)
|
||||
{
|
||||
$keys[] = $result->get_key();
|
||||
}
|
||||
|
||||
return array_unique($keys);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user