【问题标题】:How to do math operations with model fields or expressions in Agile Toolkit如何在 Agile Toolkit 中使用模型字段或表达式进行数学运算
【发布时间】:2012-07-20 17:49:48
【问题描述】:

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

【问题讨论】:

    标签: php user-interface frameworks atk4


    【解决方案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'));
    });
    

    【讨论】:

      【解决方案2】:

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

      那么你可以:

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

      【讨论】:

      • 它不起作用,字段可以,但表达式不行,这是 optput: pdo_error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'abonos' in '字段列表'模式:选择参数:数组()查询:选择(选择name from alumno where cargo.alumno_id = alumno.idalumno,(选择name from plan 其中cargo.plan_id = plan.id ) plan,fecha,fechaVencimiento,name,@,@9876543441@,@9876543440@,@987645 sum(monto) from abono where abono.cargo_id = cargo.id ) abonos,monto - abonos balance,id@2 from @987655353 /跨度>
      • 这是因为 abonos 不是列而是计算字段。您必须在 set("..") 中为 balance 字段输入完整的查询。
      猜你喜欢
      • 2012-07-19
      • 1970-01-01
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多