问题
I dont know whats wrong but my code dont output the paid amount column
$payment_tbl = TableRegistry::get("MembershipPayment");
$payments = $payment_tbl->find();
$payments->select(['payment_total'=> $payments->func()->sum('paid_amount')]);
$this->set("payments",$payments);
and then echo this as echo $payments->payment_total;
回答1:
$payments
will be a query result object, not a single result. With this query, where you're expecting just a single row, add ->first()
after your sum
call.
In general, if you're not getting what you expect, dump the contents of the variable in question, like with pr($payments)
or debug($payments)
, that will very often very quickly give you a clear indication of what the problem is. In this case, you'll see it's not the Entity object that you're expecting.
来源:https://stackoverflow.com/questions/61278984/cakephp-how-to-query-the-paid-amount