1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleCrud\Queries\Mysql; |
4
|
|
|
|
5
|
|
|
use SimpleCrud\SimpleCrudException; |
6
|
|
|
use SimpleCrud\Queries\BaseQuery; |
7
|
|
|
use SimpleCrud\Queries\ExtendedSelectionTrait; |
8
|
|
|
use SimpleCrud\RowCollection; |
9
|
|
|
use SimpleCrud\Entity; |
10
|
|
|
use PDOStatement; |
11
|
|
|
use PDO; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Manages a database select query. |
15
|
|
|
*/ |
16
|
|
|
class Select extends BaseQuery |
17
|
|
|
{ |
18
|
|
|
const MODE_ONE = 1; |
19
|
|
|
const MODE_ALL = 2; |
20
|
|
|
|
21
|
|
|
use ExtendedSelectionTrait; |
22
|
|
|
|
23
|
|
|
protected $leftJoin = []; |
24
|
|
|
protected $orderBy = []; |
25
|
|
|
protected $statement; |
26
|
|
|
protected $mode; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Change the mode to returns just the first row |
30
|
|
|
* |
31
|
|
|
* @return self |
32
|
|
|
*/ |
33
|
|
|
public function one() |
34
|
|
|
{ |
35
|
|
|
$this->mode = self::MODE_ONE; |
36
|
|
|
|
37
|
|
|
return $this->limit(1); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Change the mode to returns all rows (even duplicated) |
42
|
|
|
* |
43
|
|
|
* @return self |
44
|
|
|
*/ |
45
|
|
|
public function all() |
46
|
|
|
{ |
47
|
|
|
$this->mode = self::MODE_ALL; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
* |
55
|
|
|
* @return Row|RowCollection|null |
56
|
|
|
*/ |
57
|
|
|
public function run() |
58
|
|
|
{ |
59
|
|
|
$statement = $this->__invoke(); |
60
|
|
|
|
61
|
|
|
//Returns one |
62
|
|
|
if ($this->mode === self::MODE_ONE) { |
63
|
|
|
$row = $statement->fetch(); |
64
|
|
|
|
65
|
|
|
if ($row !== false) { |
66
|
|
|
return $this->entity->create($this->entity->prepareDataFromDatabase($row)); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$result = $this->entity->createCollection(); |
73
|
|
|
|
74
|
|
|
if ($this->mode === self::MODE_ALL) { |
75
|
|
|
$result->idAsKey(false); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
while (($row = $statement->fetch())) { |
79
|
|
|
$result[] = $this->entity->create($this->entity->prepareDataFromDatabase($row)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $result; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Adds an ORDER BY clause. |
87
|
|
|
* |
88
|
|
|
* @param string $orderBy |
89
|
|
|
* @param string|null $direction |
90
|
|
|
* |
91
|
|
|
* @return self |
92
|
|
|
*/ |
93
|
|
|
public function orderBy($orderBy, $direction = null) |
94
|
|
|
{ |
95
|
|
|
if (!empty($direction)) { |
96
|
|
|
$orderBy .= ' '.$direction; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->orderBy[] = $orderBy; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Adds a LEFT JOIN clause. |
106
|
|
|
* |
107
|
|
|
* @param Entity $entity |
108
|
|
|
* @param string $on |
109
|
|
|
* @param array|null $marks |
110
|
|
|
* |
111
|
|
|
* @return self |
112
|
|
|
*/ |
113
|
|
|
public function leftJoin(Entity $entity, $on = null, $marks = null) |
114
|
|
|
{ |
115
|
|
|
if ($this->entity->getRelation($entity) !== Entity::RELATION_HAS_ONE) { |
116
|
|
|
throw new SimpleCrudException("The items '{$this->entity->name}' and '{$entity->name}' are no related or cannot be joined"); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->leftJoin[] = [ |
120
|
|
|
'entity' => $entity, |
121
|
|
|
'on' => $on, |
122
|
|
|
]; |
123
|
|
|
|
124
|
|
|
if ($marks) { |
125
|
|
|
$this->marks += $marks; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
View Code Duplication |
public function __invoke() |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$statement = $this->entity->getDb()->execute((string) $this, $this->marks); |
137
|
|
|
$statement->setFetchMode(PDO::FETCH_ASSOC); |
138
|
|
|
|
139
|
|
|
return $statement; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function __toString() |
146
|
|
|
{ |
147
|
|
|
$query = 'SELECT'; |
148
|
|
|
$query .= ' '.static::buildFields($this->entity->name, array_keys($this->entity->fields)); |
149
|
|
|
|
150
|
|
|
foreach ($this->leftJoin as $join) { |
151
|
|
|
$query .= ', '.static::buildFields($join['entity']->name, array_keys($join['entity']->fields), true); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$query .= $this->fieldsToString(); |
155
|
|
|
$query .= ' FROM `'.$this->entity->name.'`'; |
156
|
|
|
$query .= $this->fromToString(); |
157
|
|
|
|
158
|
|
|
foreach ($this->leftJoin as $join) { |
159
|
|
|
$query .= ' LEFT JOIN `'.$join['entity']->name.'`"'; |
160
|
|
|
|
161
|
|
|
if (!empty($join['on'])) { |
162
|
|
|
$query .= ' ON ('.$join['on'].')'; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$query .= $this->whereToString(); |
167
|
|
|
|
168
|
|
|
if (!empty($this->orderBy)) { |
169
|
|
|
$query .= ' ORDER BY '.implode(', ', $this->orderBy); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$query .= $this->limitToString(); |
173
|
|
|
|
174
|
|
|
return $query; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Generates the fields/tables part of a SELECT query. |
179
|
|
|
* |
180
|
|
|
* @param string $table |
181
|
|
|
* @param array $fields |
182
|
|
|
* @param bool $rename |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
protected static function buildFields($table, array $fields, $rename = false) |
187
|
|
|
{ |
188
|
|
|
$query = []; |
189
|
|
|
|
190
|
|
|
foreach ($fields as $field) { |
191
|
|
|
if ($rename) { |
192
|
|
|
$query[] = "`{$table}`.`{$field}` as `{$table}.{$field}`"; |
193
|
|
|
} else { |
194
|
|
|
$query[] = "`{$table}`.`{$field}`"; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return implode(', ', $query); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.