1

I have implemented virtual filed in model/entity/Order.php .

But i want to access for one page only ,i don't want it to be called for all the functions .So in controller how can i access virtual field so that it will be applicable for only the portion i need.

In cakephp 2x version ,i have made for controller ,but this time in 3X i am unable to make it.

below i have attached some codes

Any suggestion will be appreciated. Thank you .

Model/Entity/Order.php

 protected $_virtual = ['amount'];

    protected function _getAmount() {


            $data = [];
            $data['due'] = $this->_properties['collection']['due_amount'];
            $data['paid'] = $this->_properties['collection']['total_sale_amount'] - $this->_properties['collection']['due_amount'];
            return $data;
        }

Codes in controller

   $Lists = $this->Orders->find('all')->where($condition)->contain(['Collections','Customers'=> ['queryBuilder' => function ($q) {
                               return $q->select(['id','center_name']);
                              }],])->order(['Orders.due_date ASC']);
sradha
  • 2,216
  • 1
  • 28
  • 48

1 Answers1

2

You have used getter method of entity by declaring function _get*. Your getter method name is _getAmount(), so you can access this by entity object in controller $entity->amount();

$Lists = $this->Orders->find('all')->where($condition)->contain(['Collections','Customers'=> ['queryBuilder' => function ($q) {
                               return $q->select(['id','center_name']);
                              }],])->order(['Orders.due_date ASC']);

// Iteration will execute the query.
foreach ($Lists as $entity) {
        echo $entity->amount;
}

Check document about virtual field in CakePHP 3.x

Also no need of below line in Entity, so remove it, because you are using getter method.

protected $_virtual = ['amount'];
Haresh Vidja
  • 8,340
  • 3
  • 25
  • 42
  • But its throwing some error like Error: Call to undefined method App\Model\Entity\Order::amount() – sradha Sep 15 '16 at 09:40
  • can you debug ($entity) in foreach loop which type of object it has? – Haresh Vidja Sep 15 '16 at 09:45
  • Ok sure. I will – sradha Sep 15 '16 at 09:48
  • 1
    ohh.. use `$entity->amount` instead of `$entity->amount()` – Haresh Vidja Sep 15 '16 at 09:48
  • yes i got it, but one more problem ,how to get it in one array. I mean the result should be merge with the main array individually . So that it is easy to access for me. – sradha Sep 15 '16 at 09:56
  • which result you are talking about its a return from _getAmount() or query result into array? if you want to query result in array then `$Lists = $this->Orders->find('all')->where($condition)->contain(['Collections','Customers'=> ['queryBuilder' => function ($q) { return $q->select(['id','center_name']); }],])->order(['Orders.due_date ASC'])->toArray();` Also you need to undertand concept of Entity in CakePHP v3.x – Haresh Vidja Sep 15 '16 at 09:59
  • I want it should be come like [ { "id": 20, "customer": { "id": 5, "center_name": "Vision Care Eye Hospital" }, "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" }, "amount": { "due": 70, "paid": 40 } }, – sradha Sep 15 '16 at 10:02
  • You can Convert entity into array by using `$entity->toArray()`, but unfortunately you can't wrap virtual field in to array – Haresh Vidja Sep 15 '16 at 10:04
  • yes i want to wrap it. Previously i have wrpped the virtual fields ,but while in controller ,i am getting the result ,but i need the wrapping section . – sradha Sep 15 '16 at 10:07
  • I got the basic idea .Thank you. I accepted your work. – sradha Sep 15 '16 at 10:14
  • is it possible to do wrapping?? i need it urgently. – sradha Sep 15 '16 at 10:15
  • in new version v3 it not possible you can retrive protected and vritual fields only through Entity object not from Array – Haresh Vidja Sep 15 '16 at 10:30