我在model / entity / Order.php中实现了虚拟文件。
但我只想访问一个页面,我不希望它被调用所有功能。所以在控制器中如何访问虚拟字段,以便它只适用于我需要的部分
在cakephp 2x版本中,我已经为控制器做了,但这次是3X我无法做到。
下面我附上了一些代码
任何建议将不胜感激。 谢谢。
模型/实体/ 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;
}
控制器中的代码
$Lists = $this->Orders->find('all')->where($condition)->contain(['Collections','Customers'=> ['queryBuilder' => function ($q) {
return $q->select(['id','center_name']);
}],])->order(['Orders.due_date ASC']);
答案 0 :(得分:2)
您通过声明函数_get*
使用了实体的getter方法。您的getter方法名称为_getAmount()
,因此您可以通过控制器$ 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;
}
查看有关virtual field in CakePHP 3.x
的文件在Entity中也不需要下面的行,所以删除它,因为你使用的是getter方法。
protected $_virtual = ['amount'];