1 | <?php |
||
18 | class Joiner implements JoinerContract |
||
19 | { |
||
20 | /** |
||
21 | * Processed query instance. |
||
22 | * |
||
23 | * @var \Illuminate\Database\Query\Builder |
||
24 | */ |
||
25 | protected $query; |
||
26 | |||
27 | /** |
||
28 | * Parent model. |
||
29 | * |
||
30 | * @var \Illuminate\Database\Eloquent\Model |
||
31 | */ |
||
32 | protected $model; |
||
33 | |||
34 | /** |
||
35 | * Create new joiner instance. |
||
36 | * |
||
37 | * @param \Illuminate\Database\Query\Builder |
||
38 | * @param \Illuminate\Database\Eloquent\Model |
||
39 | */ |
||
40 | public function __construct(Builder $query, Model $model) |
||
41 | { |
||
42 | $this->query = $query; |
||
43 | $this->model = $model; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Join related tables. |
||
48 | * |
||
49 | * @param string $target |
||
50 | * @param string $type |
||
51 | * @return \Illuminate\Database\Eloquent\Model |
||
52 | */ |
||
53 | public function join($target, $type = 'inner') |
||
54 | { |
||
55 | $related = $this->model; |
||
56 | |||
57 | foreach (explode('.', $target) as $segment) { |
||
58 | $related = $this->joinSegment($related, $segment, $type); |
||
59 | } |
||
60 | |||
61 | return $related; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Left join related tables. |
||
66 | * |
||
67 | * @param string $target |
||
68 | * @return \Illuminate\Database\Eloquent\Model |
||
69 | */ |
||
70 | public function leftJoin($target) |
||
74 | |||
75 | /** |
||
76 | * Right join related tables. |
||
77 | * |
||
78 | * @param string $target |
||
79 | * @return \Illuminate\Database\Eloquent\Model |
||
80 | */ |
||
81 | public function rightJoin($target) |
||
85 | |||
86 | /** |
||
87 | * Join relation's table accordingly. |
||
88 | * |
||
89 | * @param \Illuminate\Database\Eloquent\Model $parent |
||
90 | * @param string $segment |
||
91 | * @param string $type |
||
92 | * @return \Illuminate\Database\Eloquent\Model |
||
93 | */ |
||
94 | protected function joinSegment(Model $parent, $segment, $type) |
||
110 | |||
111 | /** |
||
112 | * Determine whether the related table has been already joined. |
||
113 | * |
||
114 | * @param \Illuminate\Database\Query\JoinClause $join |
||
115 | * @return boolean |
||
116 | */ |
||
117 | protected function alreadyJoined(Join $join) |
||
121 | |||
122 | /** |
||
123 | * Get the join clause for related table. |
||
124 | * |
||
125 | * @param \Illuminate\Database\Eloquent\Model $parent |
||
126 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
127 | * @param string $type |
||
128 | * @param string $table |
||
129 | * @return \Illuminate\Database\Query\JoinClause |
||
130 | */ |
||
131 | protected function getJoinClause(Model $parent, Relation $relation, $table, $type) |
||
143 | |||
144 | /** |
||
145 | * Join pivot or 'through' table. |
||
146 | * |
||
147 | * @param \Illuminate\Database\Eloquent\Model $parent |
||
148 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
149 | * @param string $type |
||
150 | * @return void |
||
151 | */ |
||
152 | protected function joinIntermediate(Model $parent, Relation $relation, $type) |
||
168 | |||
169 | /** |
||
170 | * Get pair of the keys from relation in order to join the table. |
||
171 | * |
||
172 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
173 | * @return array |
||
174 | * |
||
175 | * @throws \LogicException |
||
176 | */ |
||
177 | protected function getJoinKeys(Relation $relation) |
||
201 | } |
||
202 |