I have customer table and need to show the customer's balance that need to calculate from another table.
Now on Customer Model I have added Getter function
public function getBalance(){
$customer_id = $this->id;
$connection = Yii::$app->getDb();
$sql = "SELECT * FROM customer_transaction WHERE customer_id = ". $customer_id ." ORDER BY transaction_date ASC , id ASC";
$command = $connection->createCommand($sql);
$results = $command->queryAll();
$balance = 0;
foreach($results as $result){
$balance = $result['balance'];
}
return $balance;
}
On CustomerSearch Model I also add
public $balance
public function rules()
{
return [
[['balance'], 'safe'],
];
}
On CustomerSearch Model search function I also added this code. The balance value show correct value but balance field is not sorted DESC
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes' => [
'balance' => [
'default' => SORT_DESC
],
]
]);
$this->load($params);
Could you please tell what's wrong with my code?