如何使用Agile Toolkit中的模型字段或表达式进行数学运算 [英] How to do math operations with model fields or expressions in Agile Toolkit

查看:93
本文介绍了如何使用Agile Toolkit中的模型字段或表达式进行数学运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

atk4.2.1

我有这个模型:

class Model_Cargo extends Model_Table {
public $table='cargo';
function init(){
    parent::init();

    $this->hasOne('Alumno');
    $this->hasOne('Plan');

    $this->addField('fecha')->type('date');
    $this->addField('fechaCreacion')->type('date');
    $this->addField('fechaVencimiento')->type('date');
    $this->addField('name');
    $this->addField('monto')->type('money');
    $this->addField('cancelado')->type('boolean')->defaultValue(false);

    $this->hasMany('Abono');
    $this->addExpression('abonos')->set($this->refSQL('Abono')->sum('monto'));
   }
}

我想用两个字段进行+或-的数学运算: 我实际上想用表达式"abonos"代替字段"monto",我该怎么做?

I wanto to make a math operation + or - with two fields: I actually want to substracr field 'monto' with expression 'abonos' how do I do it?

让我们这样说:

$this->addExpression('balance')->set('monto'-'abonos');
//this does not work

我还想在这些字段相等的地方添加addCondition ...我可以这样做吗?

I also wold like to addCondition where those fields are equal... can I do that?

喜欢的东西:

$this->addCondition('monto','abonos');
//this does not work

推荐答案

我创建了一个示例,说明了如何在表达式中使用计算字段:

I have created an example illustrating how to use calculated fields in expressions:

http://agiletoolkit.org/codepad/model/def

对于您的问题,您将需要以下内容:

For your problem, you would need something like this:

$this->addExpression('balance')->set(function($m,$q){
    return $q->expr('[f1] - [f2]')
        ->setCustom('f1',$m->getElement('monto'))
        ->setCustom('f2',$m->getElement('abonos'));
});

这篇关于如何使用Agile Toolkit中的模型字段或表达式进行数学运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆