| @@ 43-69 (lines=27) @@ | ||
| 40 | * @param string $name TODO: Remove this and store as a property |
|
| 41 | * @return array |
|
| 42 | */ |
|
| 43 | public function eager(array $instances, $name) { |
|
| 44 | $this->verifyParents($instances); |
|
| 45 | $ids = static::attributeList($instances, $this->foreignKey); |
|
| 46 | ||
| 47 | $filter = array_merge($this->filter(), array( |
|
| 48 | $this->localKey => array_unique($ids) |
|
| 49 | )); |
|
| 50 | ||
| 51 | $data = $this->storage()->read($this->target->table(), $filter); |
|
| 52 | ||
| 53 | $class = get_class($this->target); |
|
| 54 | $generated = $class::generate($data); |
|
| 55 | ||
| 56 | $related = array(); |
|
| 57 | ||
| 58 | foreach ($generated as $model) { |
|
| 59 | $related[$model->id()] = $model; |
|
| 60 | } |
|
| 61 | ||
| 62 | foreach ($instances as $instance) { |
|
| 63 | $key = $instance->get($this->foreignKey); |
|
| 64 | $value = isset($related[$key]) ? array($related[$key]) : array(); |
|
| 65 | $instance->relation($name)->set($value); |
|
| 66 | } |
|
| 67 | ||
| 68 | return $instances; |
|
| 69 | } |
|
| 70 | ||
| 71 | /** |
|
| 72 | * Retrieve the related model. |
|
| @@ 35-61 (lines=27) @@ | ||
| 32 | * @param string $name TODO: Remove this and store as a property |
|
| 33 | * @return array |
|
| 34 | */ |
|
| 35 | public function eager(array $instances, $name) { |
|
| 36 | $this->verifyParents($instances); |
|
| 37 | $ids = static::attributeList($instances, 'id'); |
|
| 38 | ||
| 39 | $filter = array_merge($this->filter(), array( |
|
| 40 | $this->foreignKey => array_unique($ids) |
|
| 41 | )); |
|
| 42 | ||
| 43 | $data = $this->storage()->read($this->target->table(), $filter); |
|
| 44 | ||
| 45 | $class = get_class($this->target); |
|
| 46 | $generated = $class::generate($data); |
|
| 47 | ||
| 48 | $related = array(); |
|
| 49 | ||
| 50 | foreach ($generated as $model) { |
|
| 51 | $related[$model->get($this->foreignKey)] = $model; |
|
| 52 | } |
|
| 53 | ||
| 54 | foreach ($instances as $instance) { |
|
| 55 | $key = $instance->id(); |
|
| 56 | $value = isset($related[$key]) ? $related[$key] : array(); |
|
| 57 | $instance->relation($name)->set($value); |
|
| 58 | } |
|
| 59 | ||
| 60 | return $instances; |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * Retrieve the related model. |
|