1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleCrud\Queries\Mysql; |
4
|
|
|
|
5
|
|
|
use SimpleCrud\AbstractRow; |
6
|
|
|
use SimpleCrud\SimpleCrudException; |
7
|
|
|
use SimpleCrud\Scheme\Scheme; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Extended trait. |
11
|
|
|
* |
12
|
|
|
* @property \SimpleCrud\Table $table |
13
|
|
|
*/ |
14
|
|
|
trait ExtendedSelectionTrait |
15
|
|
|
{ |
16
|
|
|
use SelectionTrait; |
17
|
|
|
|
18
|
|
|
protected $from = []; |
19
|
|
|
protected $fields = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Adds new extra table to the query. |
23
|
|
|
* |
24
|
|
|
* @param string $table |
25
|
|
|
* |
26
|
|
|
* @return self |
27
|
|
|
*/ |
28
|
|
|
public function from($table) |
29
|
|
|
{ |
30
|
|
|
$this->from[] = $table; |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Adds a new extra field to the query. |
37
|
|
|
* |
38
|
|
|
* @param string $field |
39
|
|
|
* |
40
|
|
|
* @return self |
41
|
|
|
*/ |
42
|
|
|
public function field($field) |
43
|
|
|
{ |
44
|
|
|
$this->fields[] = $field; |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Adds a WHERE according with the relation of other table. |
51
|
|
|
* |
52
|
|
|
* @param AbstractRow $row |
53
|
|
|
* |
54
|
|
|
* @return self |
55
|
|
|
*/ |
56
|
|
|
public function relatedWith(AbstractRow $row) |
57
|
|
|
{ |
58
|
|
|
$table = $row->getTable(); |
59
|
|
|
$scheme = $this->table->getScheme(); |
60
|
|
|
|
61
|
|
View Code Duplication |
if (!isset($scheme['relations'][$table->name])) { |
|
|
|
|
62
|
|
|
throw new SimpleCrudException(sprintf('The tables %s and %s are no related', $table->name, $this->table->name)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$relation = $scheme['relations'][$table->name]; |
66
|
|
|
|
67
|
|
|
switch ($relation[0]) { |
68
|
|
|
case Scheme::HAS_ONE: |
69
|
|
|
return $this->by($relation[1], $row->id); |
70
|
|
|
|
71
|
|
|
case Scheme::HAS_MANY: |
72
|
|
|
return $this->byId($relation[1], $row->{$relation[1]}); |
|
|
|
|
73
|
|
|
|
74
|
|
|
case Scheme::HAS_MANY_TO_MANY: |
75
|
|
|
$this->from($relation[1]); |
76
|
|
|
$this->from($table->name); |
77
|
|
|
|
78
|
|
|
$this->fields[] = sprintf('`%s`.`%s`', $relation[1], $relation[3]); |
79
|
|
|
$this->where(sprintf('`%s`.`%s` = `%s`.`id`', $relation[1], $relation[2], $this->table->name)); |
80
|
|
|
$this->where(sprintf('`%s`.`%s` = `%s`.`id`', $relation[1], $relation[3], $table->name)); |
81
|
|
|
$this->where(sprintf('`%s`.`id` IN (:%s)', $table->name, $relation[3]), [':'.$relation[3] => $row->id]); |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
|
85
|
|
|
default: |
86
|
|
|
throw new SimpleCrudException(sprintf('Invalid relation type between %s and %s', $table->name, $this->table->name)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* add extra fields to the code. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function fieldsToString($prepend = ', ') |
96
|
|
|
{ |
97
|
|
|
return $this->fields ? $prepend.implode(', ', $this->fields) : ''; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* add extra fields to the code. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function fromToString($prepend = ', ') |
106
|
|
|
{ |
107
|
|
|
return $this->from ? $prepend.'`'.implode('`, `', $this->from).'`' : ''; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.