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


How to get multiple virtual field data in cakephp 3.2

我想在抓取记录中合并两个虚拟字段。但是我只做了一个虚拟字段,另一个是返回null。

我很困惑如何返回两个虚拟字段数据。首先,它来的很好。

下面是代码在模型/实体/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。

如果我保留一个而不是两个,我就得到了我应该需要的东西。

请推荐我。任何建议都将受到高度赞赏。:)

在注释中提到,你不能在一个函数中写两个返回。

你应该使用数组。将两个值放入数组中并返回一个数组。

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