0

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 . :)

sradha
  • 2,216
  • 1
  • 28
  • 48
  • 1
    You cannot write two return in a single function. second statement will never execute. – Ravi Hirani Sep 15 '16 at 05:07
  • @Ravi Hirani yes i know ,it won't work. But is there any way ,so that i can get two result at a time. – sradha Sep 15 '16 at 05:09
  • 1
    make another function and write second statement into it. because you are using return value. OR in a single function, return an array and put bot values in an array. – Ravi Hirani Sep 15 '16 at 05:11

1 Answers1

1

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;
 }
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42