| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of Hyperf.
- *
- * @link https://www.hyperf.io
- * @document https://hyperf.wiki
- * @contact group@hyperf.io
- * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
- */
- namespace Hyperf\Database\Model\Relations;
- use Hyperf\Database\Model\Collection;
- use Hyperf\Database\Model\Model;
- use Hyperf\Database\Model\Relations\Concerns\SupportsDefaultModels;
- class HasOneThrough extends HasManyThrough
- {
- use SupportsDefaultModels;
- /**
- * Get the results of the relationship.
- */
- public function getResults()
- {
- return $this->first() ?: $this->getDefaultFor($this->farParent);
- }
- /**
- * Initialize the relation on a set of 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 string $relation
- * @return array
- */
- public function match(array $models, Collection $results, $relation)
- {
- $dictionary = $this->buildDictionary($results);
- // Once we have the dictionary we can simply spin through the parent models to
- // link them up with their children using the keyed dictionary to make the
- // matching very convenient and easy work. Then we'll just return them.
- foreach ($models as $model) {
- if (isset($dictionary[$key = $model->getAttribute($this->localKey)])) {
- $value = $dictionary[$key];
- $model->setRelation(
- $relation,
- reset($value)
- );
- }
- }
- return $models;
- }
- /**
- * Make a new related instance for the given model.
- *
- * @return Model
- */
- public function newRelatedInstanceFor(Model $parent)
- {
- return $this->related->newInstance();
- }
- }
|