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