Conditions | 4 |
Paths | 4 |
Total Lines | 46 |
Lines | 10 |
Ratio | 21.74 % |
Changes | 0 |
1 | <?php |
||
11 | public function joinRelation(Table $table2): self |
||
12 | { |
||
13 | $table1 = $this->table; |
||
|
|||
14 | |||
15 | //Has One |
||
16 | if ($field = $table1->getJoinField($table2)) { |
||
17 | $this->query |
||
18 | ->join( |
||
19 | 'LEFT', |
||
20 | (string) $table2, |
||
21 | sprintf('%s = %s', $field, $table2->id) |
||
22 | ); |
||
23 | |||
24 | //Has many |
||
25 | View Code Duplication | } elseif ($field = $table->getJoinField($table1)) { |
|
26 | $this->query |
||
27 | ->join( |
||
28 | 'LEFT', |
||
29 | (string) $table2, |
||
30 | sprintf('%s = %s', $field, $table1->id) |
||
31 | ); |
||
32 | |||
33 | //Has many to many |
||
34 | } elseif ($joinTable = $table1->getJoinTable($table2)) { |
||
35 | $field1 = $joinTable->getJoinField($table1); |
||
36 | $field2 = $joinTable->getJoinField($table2); |
||
37 | |||
38 | $this->query |
||
39 | ->join( |
||
40 | 'LEFT', |
||
41 | (string) $joinTable, |
||
42 | sprintf('%s = %s', $field1, $table1->id) |
||
43 | ) |
||
44 | ->join( |
||
45 | 'LEFT', |
||
46 | (string) $table2, |
||
47 | sprintf('%s = %s', $field2, $table2->id) |
||
48 | ); |
||
49 | } else { |
||
50 | throw new RuntimeException( |
||
51 | sprintf('The tables %s and %s are not related', $table1, $table2) |
||
52 | ); |
||
53 | } |
||
54 | |||
55 | return $this; |
||
56 | } |
||
57 | } |
||
58 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: