@@ -167,13 +167,13 @@ You may safely rely on `$this->persistence` property to make choices::
167
167
if ($this->persistence instanceof \Atk4\Data\Persistence\Sql) {
168
168
169
169
// Calculating on SQL server is more efficient!!
170
- $this->addExpression('total', ' [amount] + [vat]');
170
+ $this->addExpression('total', ['expr' => ' [amount] + [vat]'] );
171
171
} else {
172
172
173
173
// Fallback
174
- $this->addCalculatedField('total', function($m) {
174
+ $this->addCalculatedField('total', ['expr' => function ($m) {
175
175
return $m->get('amount') + $m->get('vat');
176
- } );
176
+ }, 'type' => 'float'] );
177
177
}
178
178
179
179
To invoke code from `init() ` methods of ALL models (for example soft-delete logic),
@@ -251,20 +251,20 @@ Although you may make any field read-only::
251
251
252
252
There are two methods for adding dynamically calculated fields.
253
253
254
- .. php :method :: addExpression($name, $definition )
254
+ .. php :method :: addExpression($name, $seed )
255
255
256
256
Defines a field as server-side expression (e.g. SQL)::
257
257
258
- $this->addExpression('total', ' [amount] + [vat]');
258
+ $this->addExpression('total', ['expr' => ' [amount] + [vat]'] );
259
259
260
260
The above code is executed on the server (SQL) and can be very powerful.
261
261
You must make sure that expression is valid for current `$this->persistence `::
262
262
263
- $product->addExpression('discount', $this->refLink('category_id')->fieldQuery('default_discount'));
263
+ $product->addExpression('discount', ['expr' => $this->refLink('category_id')->fieldQuery('default_discount')] );
264
264
// expression as a sub-select from referenced model (Category) imported as a read-only field
265
265
// of $product model
266
266
267
- $product->addExpression('total', 'if ([is_discounted], ([amount]+ [vat])*[discount], [amount] + [vat])');
267
+ $product->addExpression('total', ['expr' => 'if ([is_discounted], ([amount] + [vat])*[discount], [amount] + [vat])'] );
268
268
// new "total" field now contains complex logic, which is executed in SQL
269
269
270
270
$product->addCondition('total', '<', 10);
@@ -273,17 +273,17 @@ You must make sure that expression is valid for current `$this->persistence`::
273
273
274
274
For the times when you are not working with SQL persistence, you can calculate field in PHP.
275
275
276
- .. php :method :: addCalculatedField($name, $callback)
276
+ .. php :method :: addCalculatedField($name, ['expr' => $callback] )
277
277
278
278
Creates new field object inside your model. Field value will be automatically
279
279
calculated by your callback method right after individual record is loaded by the model::
280
280
281
281
$this->addField('term', ['caption' => 'Repayment term in months', 'default' => 36]);
282
282
$this->addField('rate', ['caption' => 'APR %', 'default' => 5]);
283
283
284
- $this->addCalculatedField('interest', function($m) {
284
+ $this->addCalculatedField('interest', ['expr' => function ($m) {
285
285
return $m->calculateInterest();
286
- });
286
+ }, 'type' => 'float'] );
287
287
288
288
.. important :: always use argument `$m` instead of `$this` inside your callbacks. If model is to be
289
289
`clone`d, the code relying on `$this ` would reference original model, but the code using
@@ -292,14 +292,14 @@ calculated by your callback method right after individual record is loaded by th
292
292
This can also be useful for calculating relative times::
293
293
294
294
class MyModel extends Model {
295
- use HumanTiming; // See https://stackoverflow.com/questions/2915864/php-how-to-find-the-time-elapsed-since-a-date-time
295
+ use HumanTiming; // see https://stackoverflow.com/questions/2915864/php-how-to-find-the-time-elapsed-since-a-date-time
296
296
297
297
function init(): void {
298
298
parent::init();
299
299
300
- $this->addCalculatedField('event_ts_human_friendly', function($m) {
300
+ $this->addCalculatedField('event_ts_human_friendly', ['expr' => function ($m) {
301
301
return $this->humanTiming($m->get('event_ts'));
302
- });
302
+ }] );
303
303
304
304
}
305
305
}
@@ -742,6 +742,6 @@ Setting limit and sort order
742
742
You can also use \A tk4\D ata\P ersistence\S ql\E xpression or array of expressions instead of field name here.
743
743
Or even mix them together::
744
744
745
- $m->setOrder($m->expr('[net]* [vat]'));
746
- $m->setOrder([$m->expr('[net]* [vat]'), $m->expr('[closing]- [opening]')]);
747
- $m->setOrder(['net', $m->expr('[net]* [vat]', 'ref_no')]);
745
+ $m->setOrder($m->expr('[net] * [vat]'));
746
+ $m->setOrder([$m->expr('[net] * [vat]'), $m->expr('[closing] - [opening]')]);
747
+ $m->setOrder(['net', $m->expr('[net] * [vat]', 'ref_no')]);
0 commit comments