-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1eb508c
commit b188e07
Showing
5 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\EloquentHasManyDeep; | ||
|
||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; | ||
|
||
class HasOneDeep extends HasManyDeep | ||
{ | ||
use SupportsDefaultModels; | ||
|
||
/** | ||
* Get the results of the relationship. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getResults() | ||
{ | ||
return $this->first() ?: $this->getDefaultFor(end($this->throughParents)); | ||
} | ||
|
||
/** | ||
* Initialize the relation on a set of models. | ||
* | ||
* @param array $models | ||
* @param string $relation | ||
* @return array | ||
*/ | ||
public function initRelation(array $models, $relation) | ||
{ | ||
foreach ($models as $model) { | ||
$model->setRelation($relation, $this->getDefaultFor($model)); | ||
} | ||
|
||
return $models; | ||
} | ||
|
||
/** | ||
* Match the eagerly loaded results to their parents. | ||
* | ||
* @param array $models | ||
* @param \Illuminate\Database\Eloquent\Collection $results | ||
* @param string $relation | ||
* @return array | ||
*/ | ||
public function match(array $models, Collection $results, $relation) | ||
{ | ||
$dictionary = $this->buildDictionary($results); | ||
|
||
foreach ($models as $model) { | ||
if (isset($dictionary[$key = $model->getAttribute($this->localKey)])) { | ||
$model->setRelation( | ||
$relation, reset($dictionary[$key]) | ||
); | ||
} | ||
} | ||
|
||
return $models; | ||
} | ||
|
||
/** | ||
* Make a new related instance for the given model. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $parent | ||
* @return \Illuminate\Database\Eloquent\Model | ||
*/ | ||
public function newRelatedInstanceFor(Model $parent) | ||
{ | ||
return $this->related->newInstance(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use Illuminate\Database\Capsule\Manager as Capsule; | ||
use Tests\Models\Comment; | ||
use Tests\Models\Country; | ||
|
||
class HasOneDeepTest extends TestCase | ||
{ | ||
public function testLazyLoading() | ||
{ | ||
$comment = Country::first()->comment; | ||
|
||
$this->assertEquals(1, $comment->comment_pk); | ||
$sql = 'select "comments".*, "users"."country_country_pk" from "comments"' | ||
.' inner join "posts" on "posts"."post_pk" = "comments"."post_post_pk"' | ||
.' inner join "users" on "users"."user_pk" = "posts"."user_user_pk"' | ||
.' where "users"."deleted_at" is null and "users"."country_country_pk" = ? limit 1'; | ||
$this->assertEquals($sql, Capsule::getQueryLog()[1]['query']); | ||
$this->assertEquals([1], Capsule::getQueryLog()[1]['bindings']); | ||
} | ||
|
||
public function testDefault() | ||
{ | ||
$comment = Country::find(2)->comment; | ||
|
||
$this->assertInstanceOf(Comment::class, $comment); | ||
$this->assertFalse($comment->exists); | ||
} | ||
|
||
public function testEagerLoading() | ||
{ | ||
$countries = Country::with('comment')->get(); | ||
|
||
$this->assertEquals(1, $countries[0]->comment->comment_pk); | ||
$this->assertInstanceOf(Comment::class, $countries[1]->comment); | ||
$this->assertFalse($countries[1]->comment->exists); | ||
$sql = 'select "comments".*, "users"."country_country_pk" from "comments"' | ||
.' inner join "posts" on "posts"."post_pk" = "comments"."post_post_pk"' | ||
.' inner join "users" on "users"."user_pk" = "posts"."user_user_pk"' | ||
.' where "users"."deleted_at" is null and "users"."country_country_pk" in (?, ?)'; | ||
$this->assertEquals($sql, Capsule::getQueryLog()[1]['query']); | ||
$this->assertEquals([1, 2], Capsule::getQueryLog()[1]['bindings']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters