You can use YII2 Active Record to query the sum of a database column.
Here is an example that gets total cost and total revenue for a given date range with status as CREDITED:
- $query = Ledger::find();
- $this->load($params);
- $query->where(['status' => 'CREDITED']);
- $query->andWhere('date_created >= :begin_date',[':begin_date' => $this->begin_date]);
- $query->andWhere('date_created <= :end_date',[':end_date' => $this->end_date]);
- $cost = $query->sum('cost');
- $revenue = $query->sum('revenue');
what’s the purpose of this code :
$this->load($params);