Improving laravel relationship performance

This commit is contained in:
Vinícius Fragoso
2012-10-05 10:35:48 -03:00
parent 4f8a6724b0
commit f36446bd10
4 changed files with 40 additions and 24 deletions

View File

@@ -87,16 +87,17 @@ class Belongs_To extends Relationship {
{
$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)
{
return $v->get_key() == $child->$foreign;
});
$parents_hash[$parent->get_key()] = $parent;
}
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];
}
}
}