Php 如何在敏捷工具箱中使用模型字段或表达式进行数学运算
atk4.2.1 我有这个模型:Php 如何在敏捷工具箱中使用模型字段或表达式进行数学运算,php,user-interface,frameworks,atk4,Php,User Interface,Frameworks,Atk4,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->addFi
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->addExpression('balance')->set(function($m,$q){
return $q->expr('[f1] - [f2]')
->setCustom('f1',$m->getElement('monto'))
->setCustom('f2',$m->getElement('abonos'));
});
我还想加上这些字段相等的条件。。。我可以这样做吗
有些事,比如:
$this->addCondition('monto','abonos');
//this does not work
$this->addExpression(“余额”)->set(“monto-abonos”)代码>就是这样-sql表达式
然后你可以:
$this->addCondition(“余额”,“>”,0)代码>或您需要的任何内容。我创建了一个示例,演示如何在表达式中使用计算字段:
对于您的问题,您需要以下内容:
$this->addExpression('balance')->set('monto'-'abonos');
//this does not work
$this->addExpression('balance')->set(function($m,$q){
return $q->expr('[f1] - [f2]')
->setCustom('f1',$m->getElement('monto'))
->setCustom('f2',$m->getElement('abonos'));
});
它不起作用,字段可以,但表达式不行,这是optput:pdo_错误:SQLSTATE[42S22]:未找到列:1054未知列“abonos”处于“字段列表”模式:选择参数:数组()查询:选择(从alumno
中选择name
,其中cargo
alumno\u id
=id
)alumno
,(从计划中选择name
,其中cargo
计划id
计划plan
,fecha
,fechavenciiento
,name
,monto
,cancelado
,fechacreation
,(从abono
中选择sum(monto
)abonos
,monto-abonosbalance
,id
,fechaCancelacion
fromcargo
这是因为abonos不是一个列,而是一个计算字段。对于balance字段,您必须将完整的查询放入集合(“…”)。