-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
Showing
11 changed files
with
406 additions
and
7 deletions.
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
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\EloquentJsonRelations\Relations; | ||
|
||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; | ||
use Illuminate\Database\Eloquent\Relations\HasOneOrMany; | ||
|
||
class HasOneJson extends HasManyJson | ||
{ | ||
use SupportsDefaultModels; | ||
|
||
/** @inheritDoc */ | ||
public function getResults() | ||
{ | ||
if (is_null($this->getParentKey())) { | ||
return $this->getDefaultFor($this->parent); | ||
} | ||
|
||
return $this->first() ?: $this->getDefaultFor($this->parent); | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function initRelation(array $models, $relation) | ||
{ | ||
foreach ($models as $model) { | ||
$model->setRelation($relation, $this->getDefaultFor($model)); | ||
} | ||
|
||
return $models; | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function match(array $models, Collection $results, $relation) | ||
{ | ||
return $this->matchOne($models, $results, $relation); | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function matchOne(array $models, Collection $results, $relation) | ||
{ | ||
if ($this->hasCompositeKey()) { | ||
$this->matchWithCompositeKey($models, $results, $relation, 'one'); | ||
} else { | ||
HasOneOrMany::matchOneOrMany($models, $results, $relation, 'one'); | ||
} | ||
|
||
if ($this->key) { | ||
foreach ($models as $model) { | ||
$model->setRelation( | ||
$relation, | ||
$this->hydratePivotRelation( | ||
new Collection( | ||
array_filter([$model->$relation]) | ||
), | ||
$model, | ||
fn (Model $model) => $model->{$this->getPathName()} | ||
)->first() | ||
); | ||
} | ||
} | ||
|
||
return $models; | ||
} | ||
|
||
/** @inheritDoc */ | ||
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
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,85 @@ | ||
<?php | ||
|
||
namespace Tests\CompositeKeys; | ||
|
||
use Illuminate\Database\Capsule\Manager as DB; | ||
use Tests\Models\Task; | ||
use Tests\TestCase; | ||
|
||
class HasOneJsonTest extends TestCase | ||
{ | ||
public function testLazyLoading() | ||
{ | ||
$employee = Task::find(101)->employee; | ||
|
||
$this->assertEquals(121, $employee->id); | ||
} | ||
|
||
public function testLazyLoadingWithObjects() | ||
{ | ||
if (in_array($this->connection, ['sqlite', 'sqlsrv'])) { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$employee = Task::find(101)->employeeWithObjects; | ||
|
||
$this->assertEquals(121, $employee->id); | ||
$this->assertEquals(['work_stream' => ['active' => true]], $employee->pivot->getAttributes()); | ||
} | ||
|
||
public function testEmptyLazyLoading() | ||
{ | ||
DB::enableQueryLog(); | ||
|
||
$employee = (new Task())->employee; | ||
|
||
$this->assertNull($employee); | ||
$this->assertEmpty(DB::getQueryLog()); | ||
} | ||
|
||
public function testEagerLoading() | ||
{ | ||
$tasks = Task::with('employee')->get(); | ||
|
||
$this->assertEquals(121, $tasks[0]->employee->id); | ||
$this->assertEquals(122, $tasks[1]->employee->id); | ||
$this->assertNull($tasks[5]->employee); | ||
} | ||
|
||
public function testEagerLoadingWithObjects() | ||
{ | ||
if (in_array($this->connection, ['sqlite', 'sqlsrv'])) { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$tasks = Task::with('employeeWithObjects')->get(); | ||
|
||
$this->assertEquals(121, $tasks[0]->employeeWithObjects->id); | ||
$this->assertEquals(122, $tasks[1]->employeeWithObjects->id); | ||
$this->assertNull($tasks[5]->employeeWithObjects); | ||
$this->assertEquals(['work_stream' => ['active' => true]], $tasks[0]->employeeWithObjects->pivot->getAttributes()); | ||
} | ||
|
||
public function testLazyEagerLoading() | ||
{ | ||
$tasks = Task::all()->load('employee'); | ||
|
||
$this->assertEquals(121, $tasks[0]->employee->id); | ||
$this->assertEquals(122, $tasks[1]->employee->id); | ||
$this->assertNull($tasks[5]->employee); | ||
} | ||
|
||
public function testLazyEagerLoadingWithObjects() | ||
{ | ||
if (in_array($this->connection, ['sqlite', 'sqlsrv'])) { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$tasks = Task::all()->load('employeeWithObjects'); | ||
|
||
$this->assertEquals(121, $tasks[0]->employeeWithObjects->id); | ||
$this->assertEquals(122, $tasks[1]->employeeWithObjects->id); | ||
$this->assertNull($tasks[5]->employeeWithObjects); | ||
$this->assertEquals(['work_stream' => ['active' => true]], $tasks[0]->employeeWithObjects->pivot->getAttributes()); | ||
} | ||
} |
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
Oops, something went wrong.