2016-09-15 26 views
0

我希望兩個虛擬字段在提取記錄中合併。但是我只爲一個虛擬字段完成了另一個虛擬字段,另一個虛擬字段返回null。如何在cakephp中獲取多個虛擬現場數據3.2

我很混淆如何返回兩個虛擬現場數據。

下面是代碼 在模型/實體/ 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']; 
    } 

下面是輸出

[ 
    { 
      "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 
    } 
] 

這裏付出來了空,但應該來110 -70 = 40。

如果我保留任何一個,而不是2,我得到我應該需要的東西。

請給我建議。 任何建議將不勝感激。 :)

+1

您不能在單個函數中寫入兩個返回值。第二個陳述永遠不會執行。 –

+0

@Ravi Hirani是的,我知道,它不會工作。 但是有什麼辦法,這樣我就可以一次得到兩個結果。 – sradha

+1

使另一個函數,並寫入第二條語句。因爲你正在使用返回值。或者在一個函數中,返回一個數組,並將bot值放入一個數組中。 –

回答

1

在評論中提到,您不能在單個函數中編寫兩個返回值。您應該使用數組。將兩個值放在數組中並返回一個數組。

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; 
} 
+0

是的,我明白了。 非常感謝。 這是我在這裏問的愚蠢的東西。 – sradha

+0

:)再次感謝您。 – sradha

+1

不客氣:-)樂於幫忙。 –