Skip to content

Commit 3db6a8a

Browse files
committed
adjust to latest develop
1 parent 25c92cb commit 3db6a8a

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ $data = new JobReport($db);
191191
// BarChart wants aggregated data
192192
$data->addExpression('month', ['expr' => 'month([date])']);
193193
$aggregate = new AggregateModel($data);
194-
$aggregate->setGroupBy('month', ['profit_margin' => 'sum']);
194+
$aggregate->setGroupBy(['month'], [
195+
'profit_margin' => ['expr' => 'sum'],
196+
]);
195197

196198
// associate presentation with data
197199
$chart->setModel($aggregate, ['month', 'profit_margin']);

docs/aggregates.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ in various ways to fine-tune aggregation. Below is one sample use::
2525
$aggregate->addField('country');
2626
$aggregate->setGroupBy(['country_id'], [
2727
'count' => ['expr' => 'count(*)', 'type' => 'integer'],
28-
'total_amount' => ['expr' => 'sum([amount])', 'type' => 'atk4_money']
28+
'total_amount' => ['expr' => 'sum([amount])', 'type' => 'atk4_money'],
2929
],
3030
);
3131

@@ -40,6 +40,6 @@ Below is how opening balance can be built::
4040

4141
// we actually need grouping by nominal
4242
$ledger->setGroupBy(['nominal_id'], [
43-
'opening_balance' => ['expr' => 'sum([amount])', 'type' => 'atk4_money']
43+
'opening_balance' => ['expr' => 'sum([amount])', 'type' => 'atk4_money'],
4444
]);
4545

docs/overview.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ order to use grouping with Agile Data, your code would be::
206206

207207
$aggregate = new AggregateModel(new Sale($db));
208208
$aggregate->setGroupBy(['contractor_to', 'type'], [ // groups by 2 columns
209-
'c' => 'count(*)', // defines aggregate formulas for fields
210-
'qty' => 'sum([])', // [] refers back to qty
211-
'total' => 'sum([amount])', // can specify any field here
209+
'c' => ['expr' => 'count(*)'], // defines aggregate formulas for fields
210+
'qty' => ['expr' => 'sum([])'], // [] refers back to qty
211+
'total' => ['expr' => 'sum([amount])'], // can specify any field here
212212
]);
213213

214214

src/Model/AggregateModel.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
* It's quite simple to set up.
1919
*
2020
* $aggregate = new AggregateModel($mymodel);
21-
* $aggregate->setGroupBy(['first','last'], ['salary'=>'sum([])'];
21+
* $aggregate->setGroupBy(['first', 'last'], [
22+
* 'salary' => ['expr' => 'sum([])'],
23+
* ];
2224
*
2325
* your resulting model will have 3 fields:
2426
* first, last, salary
@@ -32,7 +34,9 @@
3234
* permitted to add expressions.
3335
*
3436
* You can also pass seed (for example field type) when aggregating:
35-
* $aggregate->setGroupBy(['first', 'last'], ['salary' => ['sum([])', 'type' => 'atk4_money']];
37+
* $aggregate->setGroupBy(['first', 'last'], [
38+
* 'salary' => ['expr' => 'sum([])', 'type' => 'atk4_money'],
39+
* ];
3640
*
3741
* @property Persistence\Sql $persistence
3842
* @property Model $table
@@ -90,7 +94,7 @@ public function setGroupBy(array $fields, array $aggregateExpressions = [])
9094
$args = [$this->table->getField($name)];
9195
}
9296

93-
$seed[0 /* TODO 'expr' was here, 0 fixes tests, but 'expr' in seed might this be defined */] = $this->table->expr($seed[0] ?? $seed['expr'], $args);
97+
$seed['expr'] = $this->table->expr($seed['expr'], $args);
9498

9599
// now add the expressions here
96100
$this->addExpression($name, $seed);

tests/ModelAggregateTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function testGroupSelectExpr(): void
132132
'amount' => ['expr' => 'sum([])', 'type' => 'atk4_money'],
133133
]);
134134

135-
$aggregate->addExpression('double', ['[s]+[amount]', 'type' => 'atk4_money']);
135+
$aggregate->addExpression('double', ['expr' => '[s] + [amount]', 'type' => 'atk4_money']);
136136

137137
$this->assertSameExportUnordered(
138138
[
@@ -154,7 +154,7 @@ public function testGroupSelectCondition(): void
154154
'amount' => ['expr' => 'sum([])', 'type' => 'atk4_money'],
155155
]);
156156

157-
$aggregate->addExpression('double', ['[s]+[amount]', 'type' => 'atk4_money']);
157+
$aggregate->addExpression('double', ['expr' => '[s] + [amount]', 'type' => 'atk4_money']);
158158

159159
$this->assertSameExportUnordered(
160160
[
@@ -175,7 +175,7 @@ public function testGroupSelectCondition2(): void
175175
'amount' => ['expr' => 'sum([])', 'type' => 'atk4_money'],
176176
]);
177177

178-
$aggregate->addExpression('double', ['[s]+[amount]', 'type' => 'atk4_money']);
178+
$aggregate->addExpression('double', ['expr' => '[s] + [amount]', 'type' => 'atk4_money']);
179179
$aggregate->addCondition(
180180
'double',
181181
'>',
@@ -201,7 +201,7 @@ public function testGroupSelectCondition3(): void
201201
'amount' => ['expr' => 'sum([])', 'type' => 'atk4_money'],
202202
]);
203203

204-
$aggregate->addExpression('double', ['[s]+[amount]', 'type' => 'atk4_money']);
204+
$aggregate->addExpression('double', ['expr' => '[s] + [amount]', 'type' => 'atk4_money']);
205205
$aggregate->addCondition(
206206
'double',
207207
// TODO Sqlite bind param does not work, expr needed, even if casted to float with DBAL type (comparison works only if casted to/bind as int)
@@ -226,7 +226,7 @@ public function testGroupSelectCondition4(): void
226226
'amount' => ['expr' => 'sum([])', 'type' => 'atk4_money'],
227227
]);
228228

229-
$aggregate->addExpression('double', ['[s]+[amount]', 'type' => 'atk4_money']);
229+
$aggregate->addExpression('double', ['expr' => '[s] + [amount]', 'type' => 'atk4_money']);
230230
$aggregate->addCondition('client_id', 2);
231231

232232
$this->assertSame(

0 commit comments

Comments
 (0)