0

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' 我该怎么做?

让我们这样说:

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

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

比如:

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

2 回答 2

1

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

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

对于您的问题,您需要这样的东西:

$this->addExpression('balance')->set(function($m,$q){
    return $q->expr('[f1] - [f2]')
        ->setCustom('f1',$m->getElement('monto'))
        ->setCustom('f2',$m->getElement('abonos'));
});
于 2012-07-23T11:15:20.307 回答
1

$this->addExpression("balance")->set("monto - abonos");就是这样 - sql 表达式。

那么你也能:

$this->addCondition("balance", ">", 0);或任何你需要的。

于 2012-07-22T11:50:06.070 回答