Commit 8faf38ef authored by Taylor Otwell's avatar Taylor Otwell

Merge pull request #1314 from vFragosop/eloquent/relationship_performance

Huge performance increase on Eloquents relationships
parents 4f8a6724 f36446bd
...@@ -87,16 +87,17 @@ class Belongs_To extends Relationship { ...@@ -87,16 +87,17 @@ class Belongs_To extends Relationship {
{ {
$foreign = $this->foreign_key(); $foreign = $this->foreign_key();
foreach ($children as &$child) $parents_hash = array();
foreach ($parents as $parent)
{ {
$parent = array_first($parents, function($k, $v) use (&$child, $foreign) $parents_hash[$parent->get_key()] = $parent;
{ }
return $v->get_key() == $child->$foreign;
});
if ( ! is_null($parent)) foreach ($children as $child)
{
if (array_key_exists($child->$foreign, $parents_hash))
{ {
$child->relationships[$relationship] = $parent; $child->relationships[$relationship] = $parents_hash[$child->$foreign];
} }
} }
} }
......
...@@ -91,14 +91,18 @@ class Has_Many extends Has_One_Or_Many { ...@@ -91,14 +91,18 @@ class Has_Many extends Has_One_Or_Many {
{ {
$foreign = $this->foreign_key(); $foreign = $this->foreign_key();
foreach ($parents as &$parent) $children_hash = array();
foreach ($children as $child)
{ {
$matching = array_filter($children, function($v) use (&$parent, $foreign) $children_hash[$child->$foreign][] = $child;
{ }
return $v->$foreign == $parent->get_key();
});
$parent->relationships[$relationship] = array_values($matching); foreach ($parents as $parent)
{
if (array_key_exists($parent->get_key(), $children_hash))
{
$parent->relationships[$relationship] = $children_hash[$parent->get_key()];
}
} }
} }
......
...@@ -328,14 +328,18 @@ class Has_Many_And_Belongs_To extends Relationship { ...@@ -328,14 +328,18 @@ class Has_Many_And_Belongs_To extends Relationship {
{ {
$foreign = $this->foreign_key(); $foreign = $this->foreign_key();
foreach ($parents as &$parent) $children_hash = array();
foreach ($children as $child)
{ {
$matching = array_filter($children, function($v) use (&$parent, $foreign) $children_hash[$child->pivot->$foreign][] = $child;
{ }
return $v->pivot->$foreign == $parent->get_key();
});
$parent->relationships[$relationship] = array_values($matching); foreach ($parents as $parent)
{
if (array_key_exists($parent->get_key(), $children_hash))
{
$parent->relationships[$relationship] = $children_hash[$parent->get_key()];
}
} }
} }
......
...@@ -38,14 +38,21 @@ class Has_One extends Has_One_Or_Many { ...@@ -38,14 +38,21 @@ class Has_One extends Has_One_Or_Many {
{ {
$foreign = $this->foreign_key(); $foreign = $this->foreign_key();
foreach ($parents as &$parent) $children_hash = array();
foreach ($children as $child)
{ {
$matching = array_first($children, function($k, $v) use (&$parent, $foreign) if (array_key_exists($child->pivot->$foreign, $children_hash))
{ {
return $v->$foreign == $parent->get_key(); continue;
}); }
$children_hash[$child->pivot->$foreign] = $child;
}
$parent->relationships[$relationship] = $matching; foreach ($parents as $parent)
{
if (array_key_exists($parent->get_key(), $children_hash))
$parent->relationships[$relationship] = $children_hash[$parent->get_key()];
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment