Agile toolkit - access calculated fields (expressi

2019-08-28 20:59发布

I have a model with a calculated field:

class Model_UsedItem extends Model_Table {
public $table='usedItems';

function init() {
    parent::init();
    $this->hasOne('Case', 'case_id', 'id');
    $this->hasOne('Item', 'item_id', 'description');
    $i = $this->join('Items', 'item_id');
    $i->addField('unitCost')->hidden(true);
    $this->addField('quantity')
    $this->addExpression('subTotal')->set('unitCost * quantity')->type('money');
}

(Table usedItems contains id, case_id, item_id, and quantity.The value for unitCost is stored in a separate table Items which also contains description. It is modelled as Model_Item but I did not include it here)

And here is my Model_Case:

class Model_Case extends Model_MyTable {
public $table='cases';

function init(){
    parent::init();
    $this->getElement('id')->system(false);
    $this->addField('date')->type('date');
    $this->hasMany('UsedItem', 'case_id');
}

This works perfectly fine and I can use CRUD with an expander to see each Case and list of associated UsedItem entries, along with calculated field subTotal.

But I also want Model_Case to have a field Total that is calculated from SUM(subTotal). I made it work by doing

$this->addExpression('Total')->set(function($model,$select){
    return $select->dsql()
        ->table('usedItems')->join('items','item_id')
        ->field($select->expr('SUM(quantity*unitCost)'))
        ->where('case_id',$select->getField('id'));
})->type('money');

This seems inefficient to do it like this when I have already written code for subTotal. Plus this is the simplest case where this concept would be used in my application and more complicated cases will end up completely illegible (and less efficient? I'm not exactly sure how it works).

How can I get Model_Case to access Model_UsedItem and perform another calculation on it? DSQL only acts on the tables, not the models, so that won't work here, right?

I am used to being able to do this in MS Access where queries can call each other as if they were tables, so Query1 could calculate SELECT SUM(quantity*unitCost) AS subTotal and then Query2 could do SELECT SUM(subTotal) FROM Query1

Thanks in advance!

标签: sql atk4
1条回答
Evening l夕情丶
2楼-- · 2019-08-28 21:28

I think it is answered here: https://stackoverflow.com/a/11596300/204819. what's important is that the code produces ONE query. As a parameter to sum() you can specify a field which is also an expression, it should work fine.

查看更多
登录 后发表回答