Created
January 24, 2020 05:01
-
-
Save t-kuni/eb561495480fd6b33b9105c3105c40a1 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace App\Xxx; | |
use Illuminate\Database\Eloquent\Builder; | |
use Illuminate\Database\Eloquent\Collection; | |
use Illuminate\Database\Eloquent\Relations\HasOne; | |
/** | |
* Class HasOneCustomResolver | |
* Eloquent Modelのリレーションとして、テーブルではなく任意のオブジェクトを差し込むためのクラス | |
* | |
* Eloquent Model側には以下の様にリレーションを定義する。 | |
* public function childItem() { | |
* | |
* $relation = new HasOneCustomResolver($this->newQuery(), ChildItem::class); | |
* | |
* $resolver = function(Model $parent) { | |
* // よしなに$childItemを生成して返す | |
* return new ChildItem(); | |
* }; | |
* | |
* $relation->setRelationResolver($resolver); | |
* | |
* return $relation; | |
* } | |
* | |
* @package App\Xxx | |
*/ | |
class HasOneCustomResolver extends HasOne | |
{ | |
protected $query; | |
/** | |
* @var \Closure | |
*/ | |
private $relationResolver; | |
public function __construct(Builder $query, string $relatedClass) | |
{ | |
$parent = new $relatedClass; | |
parent::__construct($query, $parent, '', ''); | |
} | |
public function setRelationResolver(\Closure $relationResolver) | |
{ | |
$this->relationResolver = $relationResolver; | |
} | |
/** | |
* Set the constraints for an eager load of the relation. | |
* | |
* @param array $models | |
* @return void | |
*/ | |
public function addEagerConstraints(array $models) | |
{ | |
// TODO: Implement addEagerConstraints() method. | |
} | |
/** | |
* Initialize the relation on a set of models. | |
* | |
* @param array $models | |
* @param string $relation | |
* @return array | |
*/ | |
public function initRelation(array $models, $relation) | |
{ | |
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) | |
{ | |
foreach ($models as $model) { | |
$model->setRelation($relation, $this->relationResolver->call($this, $model)); | |
} | |
$children = []; | |
foreach ($models as &$model) { | |
$children[] = &$model->$relation; | |
} | |
if (count($models) > 0) { | |
$query = $this->getQuery(); | |
$query->setModel($this->parent); | |
$builder = $query->applyScopes(); | |
$builder->eagerLoadRelations($children); | |
} | |
return $models; | |
} | |
/** | |
* Get the results of the relationship. | |
* | |
* @return mixed | |
*/ | |
public function getResults() | |
{ | |
// TODO: Implement match() method. | |
} | |
public function getEager() | |
{ | |
return new Collection([]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment