1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Database\Orm\Traits; |
4
|
|
|
|
5
|
|
|
use Win\Database\Connection; |
6
|
|
|
use Win\Database\Orm\Model; |
7
|
|
|
use Win\Database\Sql\Queries\Select; |
8
|
|
|
|
9
|
|
|
trait ReadTrait |
10
|
|
|
{ |
11
|
|
|
/** @var Connection */ |
12
|
|
|
protected static $db; |
13
|
|
|
|
14
|
|
|
/** @var Select */ |
15
|
|
|
private $query; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param mixed[] $row |
19
|
|
|
* @return Model |
20
|
|
|
*/ |
21
|
|
|
abstract public function mapModel($row); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Retorna o primeiro resultado da busca |
25
|
|
|
*/ |
26
|
|
|
public function one() |
27
|
|
|
{ |
28
|
|
|
$rows = $this->query->execute(); |
29
|
|
|
|
30
|
|
|
return $this->mapModel($rows[0]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Retorna todos os resultado da busca |
35
|
|
|
*/ |
36
|
|
|
public function all() |
37
|
|
|
{ |
38
|
|
|
$rows = $this->query->execute(); |
39
|
|
|
$all = []; |
40
|
|
|
foreach ($rows as $row) { |
41
|
|
|
$all[] = $this->mapModel($row); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $all; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Retorna o total de registros da busca |
49
|
|
|
* @return int |
50
|
|
|
*/ |
51
|
|
|
public function count() |
52
|
|
|
{ |
53
|
|
|
return $this->query->count(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Define as colunas da busca |
58
|
|
|
* @param string[] $columns |
59
|
|
|
* @return static |
60
|
|
|
*/ |
61
|
|
|
public function setColumns($columns) |
62
|
|
|
{ |
63
|
|
|
$this->query->columns = $columns; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Adiciona uma coluna à busca |
70
|
|
|
* @param string $column |
71
|
|
|
* @return static |
72
|
|
|
*/ |
73
|
|
|
public function addColumn($column) |
74
|
|
|
{ |
75
|
|
|
$this->query->columns[] = $column; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Filtra pelo id |
82
|
|
|
* @param int $id |
83
|
|
|
*/ |
84
|
|
|
public function find($id) |
85
|
|
|
{ |
86
|
|
|
$this->filterBy('id', $id); |
87
|
|
|
|
88
|
|
|
return $this->one(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Filtra pelo campo |
93
|
|
|
* @param string $column |
94
|
|
|
* @param mixed $value |
95
|
|
|
* @return static |
96
|
|
|
*/ |
97
|
|
|
public function filterBy($column, $value) |
98
|
|
|
{ |
99
|
|
|
$this->filter($column, '=', $value); |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Adiciona filtros à busca |
106
|
|
|
* @return static |
107
|
|
|
*/ |
108
|
|
|
public function filter($column, $operator, $value) |
109
|
|
|
{ |
110
|
|
|
$this->query->where->add($column, $operator, $value); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Limita os resultados da busca |
117
|
|
|
* @param int $limit |
118
|
|
|
* @return static |
119
|
|
|
*/ |
120
|
|
|
public function limit($limit) |
121
|
|
|
{ |
122
|
|
|
$this->query->limit->set($limit); |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|