-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryObject.php
76 lines (68 loc) · 1.86 KB
/
QueryObject.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace Core\DB;
class QueryObject extends QueryBuilder
{
private $entity;
public function __construct($entity)
{
parent::__construct();
if (is_object($entity)) {
$this->entity = $entity;
$this->entity->setQuery($this);
} else {
$className = '\\Entity\\' . $entity;
$this->entity = new $className;
$this->entity->setQuery($this);
}
$this->table = $this->entity->table();
$this->fields = $this->entity->fields();
}
public function find()
{
$this->limit(1);
$query = $this->select();
if ($res = $this->rawQuery($query)) {
$data = $res->fetchAll();
$data = reset($data);
if (empty($data)) {
return null;
}
$this->entity
->exchangeArray($data)
->cache();
$res->closeCursor();
return $this->entity;
}
return false;
}
public function getAll()
{
$query = $this->select();
$ret = [];
if ($res = $this->rawQuery($query)) {
while ($data = $res->fetch()) {
$inst = clone $this->entity;
$inst->exchangeArray($data)->cache();
$ret[] = $inst;
}
$res->closeCursor();
return $ret;
}
return false;
}
public function getInd(string $ind)
{
$query = $this->select();
$ret = [];
if ($res = $this->rawQuery($query)) {
while ($data = $res->fetch()) {
$inst = clone $this->entity;
$inst->exchangeArray($data)->cache();
$ret[$data[$ind]] = $inst;
}
$res->closeCursor();
return $ret;
}
return false;
}
}