|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SimpleCrud\Queries; |
|
5
|
|
|
|
|
6
|
|
|
use Closure; |
|
7
|
|
|
use PDO; |
|
8
|
|
|
use SimpleCrud\Row; |
|
9
|
|
|
use SimpleCrud\Table; |
|
10
|
|
|
|
|
11
|
|
|
class Select extends Query |
|
12
|
|
|
{ |
|
13
|
|
|
use Traits\HasRelatedWith; |
|
14
|
|
|
use Traits\HasPagination; |
|
15
|
|
|
use Traits\HasJoinRelation; |
|
16
|
|
|
|
|
17
|
|
|
private $one; |
|
18
|
|
|
protected const ALLOWED_METHODS = [ |
|
19
|
|
|
'from', |
|
20
|
|
|
'columns', |
|
21
|
|
|
'join', |
|
22
|
|
|
'catJoin', |
|
23
|
|
|
'groupBy', |
|
24
|
|
|
'having', |
|
25
|
|
|
'orHaving', |
|
26
|
|
|
'orderBy', |
|
27
|
|
|
'catHaving', |
|
28
|
|
|
'where', |
|
29
|
|
|
'whereEquals', |
|
30
|
|
|
'orWhere', |
|
31
|
|
|
'catWhere', |
|
32
|
|
|
'limit', |
|
33
|
|
|
'offset', |
|
34
|
|
|
'distinct', |
|
35
|
|
|
'forUpdate', |
|
36
|
|
|
'setFlag', |
|
37
|
|
|
'bindValue', |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct(Table $table) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->table = $table; |
|
43
|
|
|
|
|
44
|
|
|
$this->query = $table->getDatabase() |
|
45
|
|
|
->select() |
|
46
|
|
|
->from((string) $table); |
|
47
|
|
|
|
|
48
|
|
|
foreach ($table->getFields() as $field) { |
|
49
|
|
|
$field->select($this->query); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function one(): self |
|
54
|
|
|
{ |
|
55
|
|
|
$this->one = true; |
|
56
|
|
|
$this->query->limit(1); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function run() |
|
62
|
|
|
{ |
|
63
|
|
|
$data = $this->getArray(); |
|
64
|
|
|
|
|
65
|
|
|
if ($this->one) { |
|
66
|
|
|
return $data ? $this->createRow($data) : null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$rows = array_map(Closure::fromCallable([$this, 'createRow']), $data); |
|
70
|
|
|
|
|
71
|
|
|
return $this->table->createCollection($rows); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getArray(): ?array |
|
75
|
|
|
{ |
|
76
|
|
|
$statement = $this->__invoke(); |
|
77
|
|
|
$statement->setFetchMode(PDO::FETCH_ASSOC); |
|
78
|
|
|
|
|
79
|
|
|
if ($this->one) { |
|
80
|
|
|
return $statement->fetch() ?: null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $statement->fetchAll(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function createRow(array $data): Row |
|
87
|
|
|
{ |
|
88
|
|
|
$data = $this->table->format($data); |
|
89
|
|
|
$fields = $this->table->getFields(); |
|
90
|
|
|
$extraData = array_diff_key($data, $fields); |
|
91
|
|
|
$values = array_intersect_key($data, $fields); |
|
92
|
|
|
|
|
93
|
|
|
return $this->table->create($values)->setData($extraData); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: