|
| 1 | +<?php namespace Jenssegers\Mongodb\Relations; |
| 2 | + |
| 3 | +use Illuminate\Database\Eloquent\Model; |
| 4 | +use Illuminate\Database\Eloquent\Builder; |
| 5 | +use Illuminate\Database\Query\Expression; |
| 6 | +use Illuminate\Database\Eloquent\Collection; |
| 7 | +use Illuminate\Support\Collection as BaseCollection; |
| 8 | + |
| 9 | +class MorphTo extends BelongsTo { |
| 10 | + |
| 11 | + /** |
| 12 | + * The type of the polymorphic relation. |
| 13 | + * |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + protected $morphType; |
| 17 | + |
| 18 | + /** |
| 19 | + * The models whose relations are being eager loaded. |
| 20 | + * |
| 21 | + * @var \Illuminate\Database\Eloquent\Collection |
| 22 | + */ |
| 23 | + protected $models; |
| 24 | + |
| 25 | + /** |
| 26 | + * All of the models keyed by ID. |
| 27 | + * |
| 28 | + * @var array |
| 29 | + */ |
| 30 | + protected $dictionary = array(); |
| 31 | + |
| 32 | + /** |
| 33 | + * Create a new belongs to relationship instance. |
| 34 | + * |
| 35 | + * @param \Illuminate\Database\Eloquent\Builder $query |
| 36 | + * @param \Illuminate\Database\Eloquent\Model $parent |
| 37 | + * @param string $foreignKey |
| 38 | + * @param string $otherKey |
| 39 | + * @param string $type |
| 40 | + * @param string $relation |
| 41 | + * @return void |
| 42 | + */ |
| 43 | + public function __construct(Builder $query, Model $parent, $foreignKey, $otherKey, $type, $relation) |
| 44 | + { |
| 45 | + $this->morphType = $type; |
| 46 | + |
| 47 | + parent::__construct($query, $parent, $foreignKey, $otherKey, $relation); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Set the constraints for an eager load of the relation. |
| 52 | + * |
| 53 | + * @param array $models |
| 54 | + * @return void |
| 55 | + */ |
| 56 | + public function addEagerConstraints(array $models) |
| 57 | + { |
| 58 | + $this->buildDictionary($this->models = Collection::make($models)); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Buiild a dictionary with the models. |
| 63 | + * |
| 64 | + * @param \Illuminate\Database\Eloquent\Models $models |
| 65 | + * @return void |
| 66 | + */ |
| 67 | + protected function buildDictionary(Collection $models) |
| 68 | + { |
| 69 | + foreach ($models as $model) |
| 70 | + { |
| 71 | + if ($model->{$this->morphType}) |
| 72 | + { |
| 73 | + $this->dictionary[$model->{$this->morphType}][$model->{$this->foreignKey}][] = $model; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Match the eagerly loaded results to their parents. |
| 80 | + * |
| 81 | + * @param array $models |
| 82 | + * @param \Illuminate\Database\Eloquent\Collection $results |
| 83 | + * @param string $relation |
| 84 | + * @return array |
| 85 | + */ |
| 86 | + public function match(array $models, Collection $results, $relation) |
| 87 | + { |
| 88 | + return $models; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Get the results of the relationship. |
| 93 | + * |
| 94 | + * Called via eager load method of Eloquent query builder. |
| 95 | + * |
| 96 | + * @return mixed |
| 97 | + */ |
| 98 | + public function getEager() |
| 99 | + { |
| 100 | + foreach (array_keys($this->dictionary) as $type) |
| 101 | + { |
| 102 | + $this->matchToMorphParents($type, $this->getResultsByType($type)); |
| 103 | + } |
| 104 | + |
| 105 | + return $this->models; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Match the results for a given type to their parents. |
| 110 | + * |
| 111 | + * @param string $type |
| 112 | + * @param \Illuminate\Database\Eloquent\Collection $results |
| 113 | + * @return void |
| 114 | + */ |
| 115 | + protected function matchToMorphParents($type, Collection $results) |
| 116 | + { |
| 117 | + foreach ($results as $result) |
| 118 | + { |
| 119 | + if (isset($this->dictionary[$type][$result->getKey()])) |
| 120 | + { |
| 121 | + foreach ($this->dictionary[$type][$result->getKey()] as $model) |
| 122 | + { |
| 123 | + $model->setRelation($this->relation, $result); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Get all of the relation results for a type. |
| 131 | + * |
| 132 | + * @param string $type |
| 133 | + * @return \Illuminate\Database\Eloquent\Collection |
| 134 | + */ |
| 135 | + protected function getResultsByType($type) |
| 136 | + { |
| 137 | + $instance = $this->createModelByType($type); |
| 138 | + |
| 139 | + $key = $instance->getKeyName(); |
| 140 | + |
| 141 | + return $instance->whereIn($key, $this->gatherKeysByType($type)->all())->get(); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Gather all of the foreign keys for a given type. |
| 146 | + * |
| 147 | + * @param string $type |
| 148 | + * @return array |
| 149 | + */ |
| 150 | + protected function gatherKeysByType($type) |
| 151 | + { |
| 152 | + $foreign = $this->foreignKey; |
| 153 | + |
| 154 | + return BaseCollection::make($this->dictionary[$type])->map(function($models) use ($foreign) |
| 155 | + { |
| 156 | + return head($models)->{$foreign}; |
| 157 | + |
| 158 | + })->unique(); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Create a new model instance by type. |
| 163 | + * |
| 164 | + * @param string $type |
| 165 | + * @return \Illuminate\Database\Eloquent\Model |
| 166 | + */ |
| 167 | + public function createModelByType($type) |
| 168 | + { |
| 169 | + return new $type; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Get the dictionary used by the relationship. |
| 174 | + * |
| 175 | + * @return array |
| 176 | + */ |
| 177 | + public function getDictionary() |
| 178 | + { |
| 179 | + return $this->dictionary; |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments