From 7d25563bdf42f2e5f2751ba64de7423e159c772b Mon Sep 17 00:00:00 2001 From: Sarunas Date: Tue, 11 Mar 2014 09:57:57 +0000 Subject: [PATCH 1/2] Update MorphTo to enable polymorphic eager loading Taylor recently made a changes to model file in order to enable polymorphic eager loading. More details here https://github.com/laravel/framework/commit/6e0ebee17789431b7c83f1cdcb35213255817905. --- src/LaravelBook/Ardent/Ardent.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/LaravelBook/Ardent/Ardent.php b/src/LaravelBook/Ardent/Ardent.php index 49386fd..db25d19 100755 --- a/src/LaravelBook/Ardent/Ardent.php +++ b/src/LaravelBook/Ardent/Ardent.php @@ -13,6 +13,7 @@ use Illuminate\Container\Container; use Illuminate\Database\Capsule\Manager as DatabaseCapsule; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Events\Dispatcher; use Illuminate\Support\MessageBag; use Illuminate\Support\Facades\Input; @@ -410,14 +411,27 @@ public function morphTo($name = null, $type = null, $id = null) { $name = snake_case($caller['function']); } - // Next we will guess the type and ID if necessary. The type and IDs may also - // be passed into the function so that the developers may manually specify - // them on the relations. Otherwise, we will just make a great estimate. list($type, $id) = $this->getMorphs($name, $type, $id); - $class = $this->$type; + // If the type value is null it is probably safe to assume we're eager loading + // the relationship. When that is the case we will pass in a dummy query as + // there are multiple types in the morph and we can't use single queries. + if (is_null($class = $this->$type)) + { + return new MorphTo( + $this->newQuery(), $this, $id, null, $type, $name + ); + } - return $this->belongsTo($class, $id); + // If we are not eager loading the relatinship, we will essentially treat this + // as a belongs-to style relationship since morph-to extends that class and + // we will pass in the appropriate values so that it behaves as expected. + else + { + return new MorphTo( + with(new $class)->newQuery(), $this, $id, 'id', $type, $name + ); + } } /** From d72ae1c6b9a671c270a0d44065753cea731fc8d8 Mon Sep 17 00:00:00 2001 From: Sarunas Date: Mon, 17 Mar 2014 11:58:18 +0000 Subject: [PATCH 2/2] Update changes from laravel --- src/LaravelBook/Ardent/Ardent.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/LaravelBook/Ardent/Ardent.php b/src/LaravelBook/Ardent/Ardent.php index db25d19..1c0ccd9 100755 --- a/src/LaravelBook/Ardent/Ardent.php +++ b/src/LaravelBook/Ardent/Ardent.php @@ -428,8 +428,10 @@ public function morphTo($name = null, $type = null, $id = null) { // we will pass in the appropriate values so that it behaves as expected. else { + $instance = new $class; + return new MorphTo( - with(new $class)->newQuery(), $this, $id, 'id', $type, $name + with($instance)->newQuery(), $this, $id, $instance->getKeyName(), $type, $name ); } }