| @@ 457-481 (lines=25) @@ | ||
| 454 | * @param string $relation |
|
| 455 | * @return array |
|
| 456 | */ |
|
| 457 | public function match(array $entities, EntityCollection $results, $relation) |
|
| 458 | { |
|
| 459 | $dictionary = $this->buildDictionary($results); |
|
| 460 | ||
| 461 | $keyName = $this->relatedMap->getKeyName(); |
|
| 462 | ||
| 463 | $cache = $this->parentMapper->getEntityCache(); |
|
| 464 | ||
| 465 | // Once we have an array dictionary of child objects we can easily match the |
|
| 466 | // children back to their parent using the dictionary and the keys on the |
|
| 467 | // the parent models. Then we will return the hydrated models back out. |
|
| 468 | foreach ($entities as $entity) { |
|
| 469 | $wrapper = $this->factory->make($entity); |
|
| 470 | ||
| 471 | if (isset($dictionary[$key = $wrapper->getEntityAttribute($keyName)])) { |
|
| 472 | $collection = $this->relatedMap->newCollection($dictionary[$key]); |
|
| 473 | ||
| 474 | $wrapper->setEntityAttribute($relation, $collection); |
|
| 475 | ||
| 476 | $cache->cacheLoadedRelationResult($entity, $relation, $collection, $this); |
|
| 477 | } |
|
| 478 | } |
|
| 479 | ||
| 480 | return $entities; |
|
| 481 | } |
|
| 482 | ||
| 483 | /** |
|
| 484 | * Build model dictionary keyed by the relation's foreign key. |
|
| @@ 203-227 (lines=25) @@ | ||
| 200 | * @param string $type |
|
| 201 | * @return array |
|
| 202 | */ |
|
| 203 | protected function matchOneOrMany(array $entities, EntityCollection $results, $relation, $type) |
|
| 204 | { |
|
| 205 | $dictionary = $this->buildDictionary($results); |
|
| 206 | ||
| 207 | $cache = $this->parentMapper->getEntityCache(); |
|
| 208 | ||
| 209 | // Once we have the dictionary we can simply spin through the parent models to |
|
| 210 | // link them up with their children using the keyed dictionary to make the |
|
| 211 | // matching very convenient and easy work. Then we'll just return them. |
|
| 212 | foreach ($entities as $entity) { |
|
| 213 | $entity = $this->factory->make($entity); |
|
| 214 | ||
| 215 | $key = $entity->getEntityAttribute($this->localKey); |
|
| 216 | ||
| 217 | if (isset($dictionary[$key])) { |
|
| 218 | $value = $this->getRelationValue($dictionary, $key, $type); |
|
| 219 | ||
| 220 | $entity->setEntityAttribute($relation, $value); |
|
| 221 | ||
| 222 | $cache->cacheLoadedRelationResult($entity, $relation, $value, $this); |
|
| 223 | } |
|
| 224 | } |
|
| 225 | ||
| 226 | return $entities; |
|
| 227 | } |
|
| 228 | ||
| 229 | /** |
|
| 230 | * Get the value of a relationship by one or many type. |
|