sradha sradha - 1 year ago 82
PHP Question

How to get multiple virtual field data in cakephp 3.2

I want two virtual field to be merged in the fetching records.But i have done it for only one virtual field,another one is returning null.

I am quite confuse how to return two virtual field data.For one its coming fine.

Below is the codes
In model/Entity/Order.php

protected $_virtual = ['due','paid'];

protected function _getDue() {
return $this->_properties['collection']['due_amount'];
return $this->_properties['collection']['total_sale_amount']-$this->_properties['collection']['due_amount'];

Below is the output

"id": 20,
"collection": {
"id": 150,
"order_id": 20,
"total_sale_amount": 110,
"net_paid": 10,
"due_amount": 70,
"is_paid": 1,
"payment_mode": "DD",
"reference_num": "",
"created": "2016-09-09T00:00:00+0000"
"due": 70,
"paid": null

Here paid is coming null,but is should come 110-70 = 40 .

if i am keeping any one instead of 2 ,i am getting what i supposed to need.

Please suggest me.
any suggestion will be highly appreciated .

Answer Source

A mentioned in comment, you cannot write two returns in a single function.

You should use an array. put both values inside array and return an array.

 protected function _getDue() {
    $data = [];
    $data['due'] = $this->_properties['collection']['due_amount'];
    $data['paid'] = $this->_properties['collection']['total_sale_amount']-$this->_properties['collection']['due_amount'];
    return $data;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download