|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Magister\Services\Database\Elegant\Relations; |
|
4
|
|
|
|
|
5
|
|
|
use Magister\Services\Database\Elegant\Builder; |
|
6
|
|
|
use Magister\Services\Database\Elegant\Model; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Relation. |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class Relation |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The Elegant query builder instance. |
|
15
|
|
|
* |
|
16
|
|
|
* @var \Magister\Services\Database\Elegant\Builder |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $query; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The parent model instance. |
|
22
|
|
|
* |
|
23
|
|
|
* @var \Magister\Services\Database\Elegant\Model |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $parent; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The related model instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @var \Magister\Services\Database\Elegant\Model |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $related; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Indicates if the relation is adding constraints. |
|
36
|
|
|
* |
|
37
|
|
|
* @var bool |
|
38
|
|
|
*/ |
|
39
|
|
|
protected static $constraints = true; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Create a new relation instance. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \Magister\Services\Database\Elegant\Builder $query |
|
45
|
|
|
* @param \Magister\Services\Database\Elegant\Model $parent |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(Builder $query, Model $parent) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->query = $query; |
|
50
|
|
|
$this->parent = $parent; |
|
51
|
|
|
$this->related = $query->getModel(); |
|
52
|
|
|
|
|
53
|
|
|
$this->addConstraints(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Set the base constraints on the relation query. |
|
58
|
|
|
* |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
|
|
abstract public function addConstraints(); |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the underlying query for the relation. |
|
65
|
|
|
* |
|
66
|
|
|
* @return \Magister\Services\Database\Elegant\Builder |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getQuery() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->query; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the base query builder driving the Elegant builder. |
|
75
|
|
|
* |
|
76
|
|
|
* @return \Magister\Services\Database\Query\Builder |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getBaseQuery() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->query->getQuery(); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the parent model of the relation. |
|
85
|
|
|
* |
|
86
|
|
|
* @return \Magister\Services\Database\Elegant\Model |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getParent() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->parent; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get the related model of the relation. |
|
95
|
|
|
* |
|
96
|
|
|
* @return \Magister\Services\Database\Elegant\Model |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getRelated() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->related; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Handle dynamic method calls to the relationship. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $method |
|
107
|
|
|
* @param array $parameters |
|
108
|
|
|
* |
|
109
|
|
|
* @return mixed |
|
110
|
|
|
*/ |
|
111
|
|
|
public function __call($method, $parameters) |
|
112
|
|
|
{ |
|
113
|
|
|
$result = call_user_func_array([$this->query, $method], $parameters); |
|
114
|
|
|
|
|
115
|
|
|
if ($result === $this->query) { |
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $result; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|