Query.php
5.55 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace artbox\odoo\components;
use yii\base\Component;
use yii\db\QueryInterface;
use yii\db\QueryTrait;
class Query extends Component implements QueryInterface
{
use QueryTrait;
/**
* @var string the model to be selected from.
* @see from()
*/
public $from;
/**
* @var array the mapping of query
*/
public $mapping = [];
public $options = [ [] ];
/**
* Executes the query and returns all results as an array.
*
* @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
*
* @return array the query results. If the query results in nothing, an empty array will be returned.
*/
public function all($db = null)
{
/**
* @var Connection $db
*/
if (empty($db)) {
$db = \Yii::$app->get('odoo');
}
return $db->createCommand($this->from, 'search_read', $this->options, $this->mapping)
->execute();
}
/**
* Executes the query and returns a single row of result.
*
* @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
*
* @return array|bool the first row (in terms of an array) of the query result. False is returned if the query
* results in nothing.
*/
public function one($db = null)
{
$result = $this->all($db);
if (empty($result)) {
return null;
} else {
return $result[ 0 ];
}
}
/**
* Returns the number of records.
*
* @param string $q the COUNT expression. Defaults to '*'.
* @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
*
* @return int number of records.
*/
public function count($q = '*', $db = null)
{
/**
* @var Connection $db
*/
if (empty($db)) {
$db = \Yii::$app->get('odoo');
}
return ( new Command(
[
'db' => $db,
]
) )->count('product.product', $this->options, $this->mapping);
}
/**
* Returns a value indicating whether the query result contains any row of data.
*
* @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
*
* @return bool whether the query result contains any row of data.
*/
public function exists($db = null)
{
if ($this->count('*', $db)) {
return true;
} else {
return false;
}
}
/**
* @param array $fields
*
* @return \artbox\odoo\components\Query
*/
public function select(array $fields)
{
$this->mapping[ 'fields' ] = $fields;
return $this;
}
/**
* @param $model
*
* @return \artbox\odoo\components\Query
*/
public function from($model)
{
$this->from = $model;
return $this;
}
/**
* @param array $mapping
*
* @return \artbox\odoo\components\Query
*/
public function mapping(array $mapping)
{
$this->mapping = $mapping;
return $this;
}
/**
* @param array $mapping
*
* @return \artbox\odoo\components\Query
*/
public function addMapping(array $mapping)
{
if (is_array($this->mapping)) {
$this->mapping = array_merge($this->mapping, $mapping);
} else {
$this->mapping = $mapping;
}
return $this;
}
/**
* @param int|null $offset
*
* @return \artbox\odoo\components\Query
*/
public function offset($offset)
{
$this->mapping[ 'offset' ] = $offset;
return $this;
}
/**
* @param int|null $limit
*
* @return \artbox\odoo\components\Query
*/
public function limit($limit)
{
$this->mapping[ 'limit' ] = $limit;
return $this;
}
/**
* @param array|string $condition
*
* @return \artbox\odoo\components\Query
*/
public function where($condition)
{
$this->options[ 0 ][] = $condition;
return $this;
}
/**
* @param array|string $condition
*
* @return \artbox\odoo\components\Query
*/
public function andWhere($condition)
{
return $this->where($condition);
}
}