Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在敏捷工具箱中使用模型字段或表达式进行数学运算_Php_User Interface_Frameworks_Atk4 - Fatal编程技术网

Php 如何在敏捷工具箱中使用模型字段或表达式进行数学运算

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

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->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-abonos
balance
id
fechaCancelacion
from
cargo
这是因为abonos不是一个列,而是一个计算字段。对于balance字段,您必须将完整的查询放入集合(“…”)。