【问题标题】:How to add a field calculated from another model table in Agile Toolkit?如何在 Agile Toolkit 中添加从另一个模型表计算的字段?
【发布时间】:2012-07-19 08:19:33
【问题描述】:

atk4.2.1

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

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???

【问题讨论】:

  • 我能够通过这个(我在开发组的某个地方得到它)获得模型中的 CURRENT ID:$this->_dsql()->getField('id')

标签: php user-interface frameworks atk4


【解决方案1】:

知道了! 它以某种方式记录在这里:http://agiletoolkit.org/learn/understand/model/intro

这是正确的做法: Model_Invoice 内部:

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

现在我真的明白了 hasMany() 的作用是什么

【讨论】:

    猜你喜欢
    • 2012-07-20
    • 1970-01-01
    • 2021-10-10
    • 2021-12-31
    • 2013-05-11
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    相关资源
    最近更新 更多