From 03b58e8ec6ff19ebc9d072a26e7b8be3f53dd9ef Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 18 Aug 2011 22:25:56 -0500 Subject: [PATCH] fixed belongs_to eager loading bug. --- system/db/eloquent/hydrator.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/system/db/eloquent/hydrator.php b/system/db/eloquent/hydrator.php index 6c7b3d6c..682bd4b3 100644 --- a/system/db/eloquent/hydrator.php +++ b/system/db/eloquent/hydrator.php @@ -66,13 +66,15 @@ private static function base($class, $results) */ private static function eagerly($eloquent, &$parents, $include) { - // We temporarily spoof the belongs_to key to allow the query to be fetched without - // any problems, since the belongs_to method actually gets the attribute. - $eloquent->attributes[$spoof = $include.'_id'] = 0; + // We temporarily spoof the query attributes to allow the query to be fetched without + // any problems, since the belongs_to method actually gets the related attribute. + $first = reset($parents); + + $eloquent->attributes = $first->attributes; $relationship = $eloquent->$include(); - unset($eloquent->attributes[$spoof]); + $eloquent->attributes = array(); // Reset the WHERE clause and bindings on the query. We'll add our own WHERE clause soon. // This will allow us to load a range of related models instead of only one.