我希望即时创建虚拟字段。
我的订单和订单详情就像...
//Order Model
class Order extends AppModel {
public $name = 'Order';
public $actsAs = array('Containable');
public $hasMany = array(
'OrderDetail' => array(
'className' => 'OrderDetail',
'foreignKey' => 'order_id',
'dependent' => true
),
);
}
//OrderDetail Model
class OrderDetail extends AppModel {
public $name = 'OrderDetail';
public $actsAs = array('Containable');
public $belongsTo = array(
'Order' => array(
'className' => 'Order',
'foreignKey' => 'order_id',
'dependent' => true
),
);
}
模型关系是这样的。我正在动态创建虚拟字段,以便我可以计算项目数量及其成本以按顺序分页。我知道奇怪的方式requestAction
。他们是我可以执行任务的任何其他方式吗?
我尝试过的代码是
$this->Order->virtualFields['totalCost'] = 0;
$this->Order->virtualFields['totalItem'] = 0;
$fields = array('Order.id', 'Order.order_key', 'Order.delivery_date','COUNT(`OrderDetail`.`order_id`) AS `Order__totalItem`', 'SUM(`OrderDetail`.`cost`) AS `Order__totalCost`');
$this->paginate['Order'] = array("fields"=>$fields, 'conditions' => array("AND" => array($condition, "Order.status" => 3)), 'limit' => '50', 'order' => array('Order.id' => 'DESC'));
它以 mysql not found 列结束Unknown column 'OrderDetail.order_id' in 'field list'
。我没有使用递归或绑定我的模型来寻找其他东西。为什么会产生这个错误?我怎样才能达到我的目的。