atk4.2.1
我有两个模型Invoice和Payment,我想在发票中添加一个字段(表达式),我可以计算已经支付的金额(可以有几个部分付款),我添加了一个联接和一个表达式,如果它们不起作用,那么写该表达式的正确方法是什么?
class Model_Invoice extends Model_Table {
public $table='invoice';
function init(){
parent::init();
$this->hasOne('Customer');
$this->hasOne('Plan');
$this->addField('date')->type('date');
$this->addField('amount')->type('money');
$this->addField('cancelled')->type('boolean')->defaultValue(false);
$this->join('payment','id');
$this->addExpression('amountPaid')->set('sum(payment.amount)')
//*****above expression is not working*********//
}
}
class Model_Payment extends Model_Table {
public $table='payment';
function init(){
parent::init();
$this->hasOne('Invoice');
$this->addField('date')->type('date');
$this->addField('concept');
$this->addField('amount')->type('money');
}
}
我可以用这样的表达:
$this->addExpression('amountPaid')->set('(SELECT sum(amount) FROM payment where invoice_id=**CURRENT ID**)');
但是如何在模型中获得CURRENT ID???
明白了!不知何故,它被记录在这里:http://agiletoolkit.org/learn/understand/model/intro
这是正确的方法:内部型号_语音:
$this->hasMany('Payment');
$this->addExpression('amountPaid')->set($this->refSQL('Payment')->sum('amount'));
现在我真正理解了hasMany()的用途