1

atk4.2.1

我有两个模型发票和付款,我想在发票中添加一个字段(表达式),我可以计算已经支付的金额(它可以有几个部分付款),我添加了一个连接和一个表达式,他们没有工作,写那个表达式的正确方法是什么?

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**)');

但是我如何获得模型中的当前 ID???

4

1 回答 1

2

Got it! It was somehow documented here: http://agiletoolkit.org/learn/understand/model/intro

This is the right way to do it: Inside Model_Invoice:

    $this->hasMany('Payment');
    $this->addExpression('amountPaid')->set($this->refSQL('Payment')->sum('amount'));

Now I really understood what's the porpuse of hasMany()

于 2012-07-21T22:22:56.907 回答