如何在cakephp 3.2中获取多个虚拟字段数据

时间:2016-09-15 05:06:01

标签: php cakephp-3.x cakephp-3.2

我希望在提取记录中合并两个虚拟字段。但是我只为一个虚拟字段完成了它,另一个虚拟字段返回null。

我很混淆如何返回两个虚拟现场数据。对于即将到来的一个。

以下是代码 在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'];
    }

以下是输出

[
    {
          "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 :(得分: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;
 }