@@ -8,218 +8,218 @@ |
||
| 8 | 8 | |
| 9 | 9 | class MorphTo extends BelongsTo |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The type of the polymorphic relation. |
|
| 13 | - * |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $morphType; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * The entities whose relations are being eager loaded. |
|
| 20 | - * |
|
| 21 | - * @var EntityCollection |
|
| 22 | - */ |
|
| 23 | - protected $entities; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * All of the result sets keyed by ID. |
|
| 27 | - * |
|
| 28 | - * @var array |
|
| 29 | - */ |
|
| 30 | - protected $dictionary = []; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Indicates if soft-deleted model instances should be fetched. |
|
| 34 | - * |
|
| 35 | - * @var bool |
|
| 36 | - */ |
|
| 37 | - protected $withTrashed = false; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Indicate if the parent entity hold the key for the relation. |
|
| 41 | - * |
|
| 42 | - * @var boolean |
|
| 43 | - */ |
|
| 44 | - protected static $ownForeignKey = true; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Create a new belongs to relationship instance. |
|
| 48 | - * |
|
| 49 | - * @param Mapper $mapper |
|
| 50 | - * @param \Analogue\ORM\Mappable $parent |
|
| 51 | - * @param string $foreignKey |
|
| 52 | - * @param string $otherKey |
|
| 53 | - * @param string $type |
|
| 54 | - * @param string $relation |
|
| 55 | - */ |
|
| 56 | - public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $type, $relation) |
|
| 57 | - { |
|
| 58 | - $this->morphType = $type; |
|
| 59 | - |
|
| 60 | - parent::__construct($mapper, $parent, $foreignKey, $otherKey, $relation); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Set the constraints for an eager load of the relation. |
|
| 65 | - * |
|
| 66 | - * @param array $results |
|
| 67 | - * @return void |
|
| 68 | - */ |
|
| 69 | - public function addEagerConstraints(array $results) |
|
| 70 | - { |
|
| 71 | - $this->buildDictionary($results); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Build a dictionary with the entities |
|
| 76 | - * |
|
| 77 | - * @param array $results |
|
| 78 | - * @return void |
|
| 79 | - */ |
|
| 80 | - protected function buildDictionary($results) |
|
| 81 | - { |
|
| 82 | - foreach($results as $result) { |
|
| 83 | - if($result[$this->morphType]) { |
|
| 84 | - $this->dictionary[$result[$this->morphType]][$result[$this->foreignKey]] = $result; |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Match the eagerly loaded results to their parents. |
|
| 91 | - * |
|
| 92 | - * @param array $results |
|
| 93 | - * @param string $relation |
|
| 94 | - * @return array |
|
| 95 | - */ |
|
| 96 | - public function match(array $results, $relation) |
|
| 97 | - { |
|
| 98 | - return $entities; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Get the results of the relationship. |
|
| 103 | - * |
|
| 104 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 105 | - * @return EntityCollection |
|
| 106 | - */ |
|
| 107 | - public function getEager() |
|
| 108 | - { |
|
| 109 | - foreach (array_keys($this->dictionary) as $type) { |
|
| 110 | - $this->matchToMorphParents($type, $this->getResultsByType($type)); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - return $this->entities; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Match the results for a given type to their parents. |
|
| 118 | - * |
|
| 119 | - * @param string $type |
|
| 120 | - * @param EntityCollection $results |
|
| 121 | - * @return void |
|
| 122 | - */ |
|
| 123 | - protected function matchToMorphParents($type, EntityCollection $results) |
|
| 124 | - { |
|
| 125 | - $mapper = $this->relatedMapper->getManager()->mapper($type); |
|
| 126 | - $keyName = $mapper->getEntityMap()->getKeyName(); |
|
| 127 | - |
|
| 128 | - foreach ($results as $result) { |
|
| 129 | - $key = $result[$keyName]; |
|
| 130 | - |
|
| 131 | - if (isset($this->dictionary[$type][$key])) { |
|
| 132 | - foreach ($this->dictionary[$type][$key] as $result) { |
|
| 133 | - $result[$this->relation] = $result; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Get all of the relation results for a type. |
|
| 141 | - * |
|
| 142 | - * @param string $type |
|
| 143 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 144 | - * @return EntityCollection |
|
| 145 | - */ |
|
| 146 | - protected function getResultsByType($type) |
|
| 147 | - { |
|
| 148 | - $mapper = $this->relatedMapper->getManager()->mapper($type); |
|
| 149 | - |
|
| 150 | - $key = $mapper->getEntityMap()->getKeyName(); |
|
| 151 | - |
|
| 152 | - $query = $mapper->getQuery(); |
|
| 153 | - |
|
| 154 | - return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get(); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Gather all of the foreign keys for a given type. |
|
| 159 | - * |
|
| 160 | - * @param string $type |
|
| 161 | - * @return BaseCollection |
|
| 162 | - */ |
|
| 163 | - protected function gatherKeysByType($type) |
|
| 164 | - { |
|
| 165 | - $foreign = $this->foreignKey; |
|
| 166 | - |
|
| 167 | - return BaseCollection::make($this->dictionary[$type])->map(function ($entities) use ($foreign) { |
|
| 168 | - return head($entities)->{$foreign}; |
|
| 169 | - |
|
| 170 | - })->unique(); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Associate the model instance to the given parent. |
|
| 175 | - * |
|
| 176 | - * @param mixed $entity |
|
| 177 | - * @return void |
|
| 178 | - */ |
|
| 179 | - public function associate($entity) |
|
| 180 | - { |
|
| 181 | - // The Mapper will retrieve this association within the object model, we won't be using |
|
| 182 | - // the foreign key attribute inside the parent Entity. |
|
| 183 | - // |
|
| 184 | - //$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey)); |
|
| 185 | - // |
|
| 186 | - // Instead, we'll just add the object to the Entity's attribute |
|
| 187 | - |
|
| 188 | - $this->parent->setEntityAttribute($this->relation, $entity->getEntityObject()); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Get the foreign key value pair for a related object |
|
| 193 | - * |
|
| 194 | - * @var mixed $related |
|
| 195 | - * |
|
| 196 | - * @return array |
|
| 197 | - */ |
|
| 198 | - public function getForeignKeyValuePair($related) |
|
| 199 | - { |
|
| 200 | - $foreignKey = $this->getForeignKey(); |
|
| 201 | - |
|
| 202 | - if ($related) { |
|
| 203 | - $wrapper = $this->factory->make($related); |
|
| 204 | - |
|
| 205 | - $relatedKey = $this->relatedMap->getKeyName(); |
|
| 206 | - |
|
| 207 | - return [ |
|
| 208 | - $foreignKey => $wrapper->getEntityAttribute($relatedKey), |
|
| 209 | - $this->morphType => $wrapper->getMap()->getMorphClass(), |
|
| 210 | - ]; |
|
| 211 | - } else { |
|
| 212 | - return [$foreignKey => null]; |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Get the dictionary used by the relationship. |
|
| 218 | - * |
|
| 219 | - * @return array |
|
| 220 | - */ |
|
| 221 | - public function getDictionary() |
|
| 222 | - { |
|
| 223 | - return $this->dictionary; |
|
| 224 | - } |
|
| 11 | + /** |
|
| 12 | + * The type of the polymorphic relation. |
|
| 13 | + * |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $morphType; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * The entities whose relations are being eager loaded. |
|
| 20 | + * |
|
| 21 | + * @var EntityCollection |
|
| 22 | + */ |
|
| 23 | + protected $entities; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * All of the result sets keyed by ID. |
|
| 27 | + * |
|
| 28 | + * @var array |
|
| 29 | + */ |
|
| 30 | + protected $dictionary = []; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Indicates if soft-deleted model instances should be fetched. |
|
| 34 | + * |
|
| 35 | + * @var bool |
|
| 36 | + */ |
|
| 37 | + protected $withTrashed = false; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Indicate if the parent entity hold the key for the relation. |
|
| 41 | + * |
|
| 42 | + * @var boolean |
|
| 43 | + */ |
|
| 44 | + protected static $ownForeignKey = true; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Create a new belongs to relationship instance. |
|
| 48 | + * |
|
| 49 | + * @param Mapper $mapper |
|
| 50 | + * @param \Analogue\ORM\Mappable $parent |
|
| 51 | + * @param string $foreignKey |
|
| 52 | + * @param string $otherKey |
|
| 53 | + * @param string $type |
|
| 54 | + * @param string $relation |
|
| 55 | + */ |
|
| 56 | + public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $type, $relation) |
|
| 57 | + { |
|
| 58 | + $this->morphType = $type; |
|
| 59 | + |
|
| 60 | + parent::__construct($mapper, $parent, $foreignKey, $otherKey, $relation); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Set the constraints for an eager load of the relation. |
|
| 65 | + * |
|
| 66 | + * @param array $results |
|
| 67 | + * @return void |
|
| 68 | + */ |
|
| 69 | + public function addEagerConstraints(array $results) |
|
| 70 | + { |
|
| 71 | + $this->buildDictionary($results); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Build a dictionary with the entities |
|
| 76 | + * |
|
| 77 | + * @param array $results |
|
| 78 | + * @return void |
|
| 79 | + */ |
|
| 80 | + protected function buildDictionary($results) |
|
| 81 | + { |
|
| 82 | + foreach($results as $result) { |
|
| 83 | + if($result[$this->morphType]) { |
|
| 84 | + $this->dictionary[$result[$this->morphType]][$result[$this->foreignKey]] = $result; |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Match the eagerly loaded results to their parents. |
|
| 91 | + * |
|
| 92 | + * @param array $results |
|
| 93 | + * @param string $relation |
|
| 94 | + * @return array |
|
| 95 | + */ |
|
| 96 | + public function match(array $results, $relation) |
|
| 97 | + { |
|
| 98 | + return $entities; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Get the results of the relationship. |
|
| 103 | + * |
|
| 104 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 105 | + * @return EntityCollection |
|
| 106 | + */ |
|
| 107 | + public function getEager() |
|
| 108 | + { |
|
| 109 | + foreach (array_keys($this->dictionary) as $type) { |
|
| 110 | + $this->matchToMorphParents($type, $this->getResultsByType($type)); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + return $this->entities; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Match the results for a given type to their parents. |
|
| 118 | + * |
|
| 119 | + * @param string $type |
|
| 120 | + * @param EntityCollection $results |
|
| 121 | + * @return void |
|
| 122 | + */ |
|
| 123 | + protected function matchToMorphParents($type, EntityCollection $results) |
|
| 124 | + { |
|
| 125 | + $mapper = $this->relatedMapper->getManager()->mapper($type); |
|
| 126 | + $keyName = $mapper->getEntityMap()->getKeyName(); |
|
| 127 | + |
|
| 128 | + foreach ($results as $result) { |
|
| 129 | + $key = $result[$keyName]; |
|
| 130 | + |
|
| 131 | + if (isset($this->dictionary[$type][$key])) { |
|
| 132 | + foreach ($this->dictionary[$type][$key] as $result) { |
|
| 133 | + $result[$this->relation] = $result; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Get all of the relation results for a type. |
|
| 141 | + * |
|
| 142 | + * @param string $type |
|
| 143 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 144 | + * @return EntityCollection |
|
| 145 | + */ |
|
| 146 | + protected function getResultsByType($type) |
|
| 147 | + { |
|
| 148 | + $mapper = $this->relatedMapper->getManager()->mapper($type); |
|
| 149 | + |
|
| 150 | + $key = $mapper->getEntityMap()->getKeyName(); |
|
| 151 | + |
|
| 152 | + $query = $mapper->getQuery(); |
|
| 153 | + |
|
| 154 | + return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get(); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Gather all of the foreign keys for a given type. |
|
| 159 | + * |
|
| 160 | + * @param string $type |
|
| 161 | + * @return BaseCollection |
|
| 162 | + */ |
|
| 163 | + protected function gatherKeysByType($type) |
|
| 164 | + { |
|
| 165 | + $foreign = $this->foreignKey; |
|
| 166 | + |
|
| 167 | + return BaseCollection::make($this->dictionary[$type])->map(function ($entities) use ($foreign) { |
|
| 168 | + return head($entities)->{$foreign}; |
|
| 169 | + |
|
| 170 | + })->unique(); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Associate the model instance to the given parent. |
|
| 175 | + * |
|
| 176 | + * @param mixed $entity |
|
| 177 | + * @return void |
|
| 178 | + */ |
|
| 179 | + public function associate($entity) |
|
| 180 | + { |
|
| 181 | + // The Mapper will retrieve this association within the object model, we won't be using |
|
| 182 | + // the foreign key attribute inside the parent Entity. |
|
| 183 | + // |
|
| 184 | + //$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey)); |
|
| 185 | + // |
|
| 186 | + // Instead, we'll just add the object to the Entity's attribute |
|
| 187 | + |
|
| 188 | + $this->parent->setEntityAttribute($this->relation, $entity->getEntityObject()); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Get the foreign key value pair for a related object |
|
| 193 | + * |
|
| 194 | + * @var mixed $related |
|
| 195 | + * |
|
| 196 | + * @return array |
|
| 197 | + */ |
|
| 198 | + public function getForeignKeyValuePair($related) |
|
| 199 | + { |
|
| 200 | + $foreignKey = $this->getForeignKey(); |
|
| 201 | + |
|
| 202 | + if ($related) { |
|
| 203 | + $wrapper = $this->factory->make($related); |
|
| 204 | + |
|
| 205 | + $relatedKey = $this->relatedMap->getKeyName(); |
|
| 206 | + |
|
| 207 | + return [ |
|
| 208 | + $foreignKey => $wrapper->getEntityAttribute($relatedKey), |
|
| 209 | + $this->morphType => $wrapper->getMap()->getMorphClass(), |
|
| 210 | + ]; |
|
| 211 | + } else { |
|
| 212 | + return [$foreignKey => null]; |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Get the dictionary used by the relationship. |
|
| 218 | + * |
|
| 219 | + * @return array |
|
| 220 | + */ |
|
| 221 | + public function getDictionary() |
|
| 222 | + { |
|
| 223 | + return $this->dictionary; |
|
| 224 | + } |
|
| 225 | 225 | } |
@@ -79,8 +79,8 @@ discard block |
||
| 79 | 79 | */ |
| 80 | 80 | protected function buildDictionary($results) |
| 81 | 81 | { |
| 82 | - foreach($results as $result) { |
|
| 83 | - if($result[$this->morphType]) { |
|
| 82 | + foreach ($results as $result) { |
|
| 83 | + if ($result[$this->morphType]) { |
|
| 84 | 84 | $this->dictionary[$result[$this->morphType]][$result[$this->foreignKey]] = $result; |
| 85 | 85 | } |
| 86 | 86 | } |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | { |
| 165 | 165 | $foreign = $this->foreignKey; |
| 166 | 166 | |
| 167 | - return BaseCollection::make($this->dictionary[$type])->map(function ($entities) use ($foreign) { |
|
| 167 | + return BaseCollection::make($this->dictionary[$type])->map(function($entities) use ($foreign) { |
|
| 168 | 168 | return head($entities)->{$foreign}; |
| 169 | 169 | |
| 170 | 170 | })->unique(); |
@@ -4,30 +4,30 @@ |
||
| 4 | 4 | |
| 5 | 5 | class MorphMany extends MorphOneOrMany |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Get the results of the relationship. |
|
| 9 | - * |
|
| 10 | - * @param $relation |
|
| 11 | - * @return mixed |
|
| 12 | - */ |
|
| 13 | - public function getResults($relation) |
|
| 14 | - { |
|
| 15 | - $results = $this->query->get(); |
|
| 7 | + /** |
|
| 8 | + * Get the results of the relationship. |
|
| 9 | + * |
|
| 10 | + * @param $relation |
|
| 11 | + * @return mixed |
|
| 12 | + */ |
|
| 13 | + public function getResults($relation) |
|
| 14 | + { |
|
| 15 | + $results = $this->query->get(); |
|
| 16 | 16 | |
| 17 | - $this->cacheRelation($results, $relation); |
|
| 17 | + $this->cacheRelation($results, $relation); |
|
| 18 | 18 | |
| 19 | - return $results; |
|
| 20 | - } |
|
| 19 | + return $results; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Match the eagerly loaded results to their parents. |
|
| 24 | - * |
|
| 25 | - * @param array $results |
|
| 26 | - * @param string $relation |
|
| 27 | - * @return array |
|
| 28 | - */ |
|
| 29 | - public function match(array $results, $relation) |
|
| 30 | - { |
|
| 31 | - return $this->matchMany($results, $relation); |
|
| 32 | - } |
|
| 22 | + /** |
|
| 23 | + * Match the eagerly loaded results to their parents. |
|
| 24 | + * |
|
| 25 | + * @param array $results |
|
| 26 | + * @param string $relation |
|
| 27 | + * @return array |
|
| 28 | + */ |
|
| 29 | + public function match(array $results, $relation) |
|
| 30 | + { |
|
| 31 | + return $this->matchMany($results, $relation); |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -7,119 +7,119 @@ |
||
| 7 | 7 | |
| 8 | 8 | abstract class MorphOneOrMany extends HasOneOrMany |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * The foreign key type for the relationship. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - protected $morphType; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * The class name of the parent model. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $morphClass; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Create a new has many relationship instance. |
|
| 26 | - * |
|
| 27 | - * @param Mapper $mapper |
|
| 28 | - * @param \Analogue\ORM\Mappable $parent |
|
| 29 | - * @param string $type |
|
| 30 | - * @param string $id |
|
| 31 | - * @param string $localKey |
|
| 32 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 33 | - */ |
|
| 34 | - public function __construct(Mapper $mapper, $parent, $type, $id, $localKey) |
|
| 35 | - { |
|
| 36 | - $this->morphType = $type; |
|
| 37 | - $this->morphClass = $mapper->getManager()->getMapper($parent)->getEntityMap()->getMorphClass(); |
|
| 38 | - |
|
| 39 | - parent::__construct($mapper, $parent, $id, $localKey); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Set the base constraints on the relation query. |
|
| 44 | - * |
|
| 45 | - * @return void |
|
| 46 | - */ |
|
| 47 | - public function addConstraints() |
|
| 48 | - { |
|
| 49 | - if (static::$constraints) { |
|
| 50 | - parent::addConstraints(); |
|
| 51 | - |
|
| 52 | - $this->query->where($this->morphType, $this->morphClass); |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Get the relationship count query. |
|
| 58 | - * |
|
| 59 | - * @param Query $query |
|
| 60 | - * @param Query $parent |
|
| 61 | - * @return Query |
|
| 62 | - */ |
|
| 63 | - public function getRelationCountQuery(Query $query, Query $parent) |
|
| 64 | - { |
|
| 65 | - $query = parent::getRelationCountQuery($query, $parent); |
|
| 66 | - |
|
| 67 | - return $query->where($this->morphType, $this->morphClass); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Set the constraints for an eager load of the relation. |
|
| 72 | - * |
|
| 73 | - * @param array $results |
|
| 74 | - * @return void |
|
| 75 | - */ |
|
| 76 | - public function addEagerConstraints(array $results) |
|
| 77 | - { |
|
| 78 | - parent::addEagerConstraints($results); |
|
| 79 | - |
|
| 80 | - $this->query->where($this->morphType, $this->morphClass); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Get the foreign key "type" name. |
|
| 85 | - * |
|
| 86 | - * @return string |
|
| 87 | - */ |
|
| 88 | - public function getMorphType() |
|
| 89 | - { |
|
| 90 | - return $this->morphType; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Get the plain morph type name without the table. |
|
| 95 | - * |
|
| 96 | - * @return string |
|
| 97 | - */ |
|
| 98 | - public function getPlainMorphType() |
|
| 99 | - { |
|
| 100 | - return last(explode('.', $this->morphType)); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Get the class name of the parent model. |
|
| 105 | - * |
|
| 106 | - * @return string |
|
| 107 | - */ |
|
| 108 | - public function getMorphClass() |
|
| 109 | - { |
|
| 110 | - return $this->morphClass; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Get the foreign key as value pair for this relation |
|
| 115 | - * |
|
| 116 | - * @return array |
|
| 117 | - */ |
|
| 118 | - public function getForeignKeyValuePair() |
|
| 119 | - { |
|
| 120 | - return [ |
|
| 121 | - $this->getPlainForeignKey() => $this->getParentKey(), |
|
| 122 | - $this->getPlainMorphType() => $this->morphClass, |
|
| 123 | - ]; |
|
| 124 | - } |
|
| 10 | + /** |
|
| 11 | + * The foreign key type for the relationship. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + protected $morphType; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * The class name of the parent model. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $morphClass; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Create a new has many relationship instance. |
|
| 26 | + * |
|
| 27 | + * @param Mapper $mapper |
|
| 28 | + * @param \Analogue\ORM\Mappable $parent |
|
| 29 | + * @param string $type |
|
| 30 | + * @param string $id |
|
| 31 | + * @param string $localKey |
|
| 32 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 33 | + */ |
|
| 34 | + public function __construct(Mapper $mapper, $parent, $type, $id, $localKey) |
|
| 35 | + { |
|
| 36 | + $this->morphType = $type; |
|
| 37 | + $this->morphClass = $mapper->getManager()->getMapper($parent)->getEntityMap()->getMorphClass(); |
|
| 38 | + |
|
| 39 | + parent::__construct($mapper, $parent, $id, $localKey); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Set the base constraints on the relation query. |
|
| 44 | + * |
|
| 45 | + * @return void |
|
| 46 | + */ |
|
| 47 | + public function addConstraints() |
|
| 48 | + { |
|
| 49 | + if (static::$constraints) { |
|
| 50 | + parent::addConstraints(); |
|
| 51 | + |
|
| 52 | + $this->query->where($this->morphType, $this->morphClass); |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Get the relationship count query. |
|
| 58 | + * |
|
| 59 | + * @param Query $query |
|
| 60 | + * @param Query $parent |
|
| 61 | + * @return Query |
|
| 62 | + */ |
|
| 63 | + public function getRelationCountQuery(Query $query, Query $parent) |
|
| 64 | + { |
|
| 65 | + $query = parent::getRelationCountQuery($query, $parent); |
|
| 66 | + |
|
| 67 | + return $query->where($this->morphType, $this->morphClass); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Set the constraints for an eager load of the relation. |
|
| 72 | + * |
|
| 73 | + * @param array $results |
|
| 74 | + * @return void |
|
| 75 | + */ |
|
| 76 | + public function addEagerConstraints(array $results) |
|
| 77 | + { |
|
| 78 | + parent::addEagerConstraints($results); |
|
| 79 | + |
|
| 80 | + $this->query->where($this->morphType, $this->morphClass); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Get the foreign key "type" name. |
|
| 85 | + * |
|
| 86 | + * @return string |
|
| 87 | + */ |
|
| 88 | + public function getMorphType() |
|
| 89 | + { |
|
| 90 | + return $this->morphType; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Get the plain morph type name without the table. |
|
| 95 | + * |
|
| 96 | + * @return string |
|
| 97 | + */ |
|
| 98 | + public function getPlainMorphType() |
|
| 99 | + { |
|
| 100 | + return last(explode('.', $this->morphType)); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Get the class name of the parent model. |
|
| 105 | + * |
|
| 106 | + * @return string |
|
| 107 | + */ |
|
| 108 | + public function getMorphClass() |
|
| 109 | + { |
|
| 110 | + return $this->morphClass; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Get the foreign key as value pair for this relation |
|
| 115 | + * |
|
| 116 | + * @return array |
|
| 117 | + */ |
|
| 118 | + public function getForeignKeyValuePair() |
|
| 119 | + { |
|
| 120 | + return [ |
|
| 121 | + $this->getPlainForeignKey() => $this->getParentKey(), |
|
| 122 | + $this->getPlainMorphType() => $this->morphClass, |
|
| 123 | + ]; |
|
| 124 | + } |
|
| 125 | 125 | } |