|
1
|
|
|
<?php |
|
2
|
|
|
namespace Darya\ORM\Relation; |
|
3
|
|
|
|
|
4
|
|
|
use Darya\ORM\Record; |
|
5
|
|
|
use Darya\ORM\Relation; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Darya's belongs-to entity relation. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Chris Andrew <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class BelongsTo extends Relation { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Set the default keys for the relation if they have not yet been set. |
|
16
|
|
|
*/ |
|
17
|
|
View Code Duplication |
protected function setDefaultKeys() { |
|
|
|
|
|
|
18
|
|
|
if (!$this->foreignKey) { |
|
19
|
|
|
$this->foreignKey = $this->prepareForeignKey(get_class($this->target)); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
$this->localKey = $this->target->key(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Retrieve the default filter for this relation. |
|
27
|
|
|
* |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function defaultConstraint() { |
|
31
|
|
|
return array($this->localKey => $this->parent->get($this->foreignKey)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Eagerly load the related models for the given parent instances. |
|
36
|
|
|
* |
|
37
|
|
|
* Returns the given instances with their related models loaded. |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $instances |
|
40
|
|
|
* @param string $name TODO: Remove this and store as a property |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
|
View Code Duplication |
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. |
|
73
|
|
|
* |
|
74
|
|
|
* @return Record|null |
|
75
|
|
|
*/ |
|
76
|
|
|
public function retrieve() { |
|
77
|
|
|
if ($this->parent->get($this->foreignKey)) { |
|
78
|
|
|
return $this->one(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Associate the given model. |
|
84
|
|
|
* |
|
85
|
|
|
* @param Record $instance |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
public function associate(Record $instance) { |
|
89
|
|
|
$instance->save(); |
|
90
|
|
|
$this->set(array($instance)); |
|
91
|
|
|
$this->parent->set($this->foreignKey, $instance->id()); |
|
92
|
|
|
|
|
93
|
|
|
return $this->parent->save(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Dissociate the related model. |
|
98
|
|
|
* |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
public function dissociate() { |
|
102
|
|
|
$this->clear(); |
|
103
|
|
|
$this->parent->set($this->foreignKey, 0); |
|
104
|
|
|
|
|
105
|
|
|
return $this->parent->save(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.