@@ 7-63 (lines=57) @@ | ||
4 | ||
5 | use Analogue\ORM\EntityCollection; |
|
6 | ||
7 | class HasOne extends HasOneOrMany |
|
8 | { |
|
9 | /** |
|
10 | * Get the results of the relationship. |
|
11 | * |
|
12 | * @param $relation |
|
13 | * @return mixed |
|
14 | */ |
|
15 | public function getResults($relation) |
|
16 | { |
|
17 | $result = $this->query->first(); |
|
18 | ||
19 | $this->cacheRelation($result, $relation); |
|
20 | ||
21 | return $result; |
|
22 | } |
|
23 | ||
24 | /** |
|
25 | * Get the results of the relationship. |
|
26 | * |
|
27 | * @return mixed |
|
28 | */ |
|
29 | public function fetch() |
|
30 | { |
|
31 | return $this->query->first(); |
|
32 | } |
|
33 | ||
34 | ||
35 | /** |
|
36 | * Initialize the relation on a set of entities. |
|
37 | * |
|
38 | * @param \Analogue\ORM\Entity[] $entities |
|
39 | * @param string $relation |
|
40 | * @return array |
|
41 | */ |
|
42 | public function initRelation(array $entities, $relation) |
|
43 | { |
|
44 | foreach ($entities as $entity) { |
|
45 | $entity->setEntityAttribute($relation, null); |
|
46 | } |
|
47 | ||
48 | return $entities; |
|
49 | } |
|
50 | ||
51 | /** |
|
52 | * Match the eagerly loaded results to their parents. |
|
53 | * |
|
54 | * @param \Analogue\ORM\Entity[] $entities |
|
55 | * @param EntityCollection $results |
|
56 | * @param string $relation |
|
57 | * @return array |
|
58 | */ |
|
59 | public function match(array $entities, EntityCollection $results, $relation) |
|
60 | { |
|
61 | return $this->matchOne($entities, $results, $relation); |
|
62 | } |
|
63 | } |
|
64 |
@@ 7-54 (lines=48) @@ | ||
4 | ||
5 | use Analogue\ORM\EntityCollection; |
|
6 | ||
7 | class MorphOne extends MorphOneOrMany |
|
8 | { |
|
9 | /** |
|
10 | * Get the results of the relationship. |
|
11 | * |
|
12 | * @param $relation |
|
13 | * @return mixed |
|
14 | */ |
|
15 | public function getResults($relation) |
|
16 | { |
|
17 | $result = $this->query->first(); |
|
18 | ||
19 | $this->cacheRelation($result, $relation); |
|
20 | ||
21 | return $result; |
|
22 | } |
|
23 | ||
24 | /** |
|
25 | * Initialize the relation on a set of models. |
|
26 | * |
|
27 | * @param array $entities |
|
28 | * @param string $relation |
|
29 | * @return array |
|
30 | */ |
|
31 | public function initRelation(array $entities, $relation) |
|
32 | { |
|
33 | foreach ($entities as $entity) { |
|
34 | $entity = $this->factory->make($entity); |
|
35 | ||
36 | $entity->setEntityAttribute($relation, null); |
|
37 | } |
|
38 | ||
39 | return $entities; |
|
40 | } |
|
41 | ||
42 | /** |
|
43 | * Match the eagerly loaded results to their parents. |
|
44 | * |
|
45 | * @param array $entities |
|
46 | * @param EntityCollection $results |
|
47 | * @param string $relation |
|
48 | * @return array |
|
49 | */ |
|
50 | public function match(array $entities, EntityCollection $results, $relation) |
|
51 | { |
|
52 | return $this->matchOne($entities, $results, $relation); |
|
53 | } |
|
54 | } |
|
55 |