1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Database\Idiorm\Statements; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\DbalInterface; |
18
|
|
|
use Quantum\Exceptions\DatabaseException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Trait Result |
22
|
|
|
* @package Quantum\Libraries\Database\Idiorm\Statements |
23
|
|
|
*/ |
24
|
|
|
trait Result |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
* @throws DatabaseException |
30
|
|
|
*/ |
31
|
|
|
public function get(?int $returnType = DbalInterface::TYPE_ARRAY) |
32
|
|
|
{ |
33
|
|
|
return ($returnType == DbalInterface::TYPE_OBJECT) ? |
34
|
|
|
$this->getOrmModel()->find_many() |
|
|
|
|
35
|
|
|
: |
36
|
|
|
$this->getOrmModel()->find_array(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritDoc |
41
|
|
|
* @throws DatabaseException |
42
|
|
|
*/ |
43
|
|
|
public function findOne(int $id): DbalInterface |
44
|
|
|
{ |
45
|
|
|
$ormObject = $this->getOrmModel()->find_one($id); |
46
|
|
|
|
47
|
|
|
if ($ormObject) { |
48
|
|
|
$this->updateOrmModel($ormObject); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritDoc |
56
|
|
|
* @throws DatabaseException |
57
|
|
|
*/ |
58
|
|
|
public function findOneBy(string $column, $value): DbalInterface |
59
|
|
|
{ |
60
|
|
|
$ormObject = $this->getOrmModel()->where($column, $value)->find_one(); |
61
|
|
|
if ($ormObject) { |
62
|
|
|
$this->updateOrmModel($ormObject); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
* @throws DatabaseException |
71
|
|
|
*/ |
72
|
|
|
public function first(): DbalInterface |
73
|
|
|
{ |
74
|
|
|
$ormObject = $this->getOrmModel()->find_one(); |
75
|
|
|
if ($ormObject) { |
76
|
|
|
$this->updateOrmModel($ormObject); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
* @throws DatabaseException |
85
|
|
|
*/ |
86
|
|
|
public function count(): int |
87
|
|
|
{ |
88
|
|
|
return $this->getOrmModel()->count(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritDoc |
93
|
|
|
* @throws DatabaseException |
94
|
|
|
*/ |
95
|
|
|
public function asArray(): array |
96
|
|
|
{ |
97
|
|
|
$result = $this->getOrmModel()->as_array(); |
98
|
|
|
|
99
|
|
|
if (count($this->hidden) > 0) { |
100
|
|
|
$result = $this->setHidden($result); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritDoc |
108
|
|
|
*/ |
109
|
|
|
public function setHidden($result) |
110
|
|
|
{ |
111
|
|
|
return array_diff_key($result, array_flip($this->hidden)); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|