Multiplying Fields from Different Tables

I've got asked a question - how can you multiply two fields located in different tables, so I decided to put this example together.

Defining Models

model_product
class Model_Product extends Model_Table {
    public $table='product';
    function init(){
        parent::init();
        
        $this->addField('name');
        $this->addField('price')->type('money');

        // Automatically create SQL table if not exists
        $this->add('dynamic_model/Controller_AutoCreator');
    }
}

  
Demo
The example was executed successfully, but did not produce any output
model_product
class Model_Purchase extends Model_Table {
    public $table='purchase';
    function init(){
        parent::init();
        
        $this->addField('date')->type('date');
        $this->hasOne('Product');
        $this->addField('qty')->type('int');

        // Automatically create SQL table if not exists
        $this->add('dynamic_model/Controller_AutoCreator');
    }
}

  
Demo
The example was executed successfully, but did not produce any output

First, let's add few products. I include a simple crud here, so you can populate them yourself:

pop1
$crud=$page->add('CRUD');
$crud->setModel('Product');
if($crud->grid) {
$crud->grid->addButton('Refresh')->js('click',$crud->grid->js()->reload());
}

  
Demo
Name Price Edit Delete
fdsfsd 0.00
Test 23.00
12 123.00
Fritz 717.00
Prueba 200.00
jlkjkl 8,888,888.00
11 99,999,999.99
111 0.00

Next, let's see how the purchase population looks:

pop2
$crud=$page->add('CRUD');
$crud->setModel('Purchase');
if($crud->grid) {
$crud->grid->addButton('Refresh')->js('click',$crud->grid->js()->reload());
}

  
Demo
Date Product Qty Edit Delete
13/06/2013 Fritz 5
13/06/2013 Fritz 3
01/05/2013 Fritz 2
01/05/2013 Prueba 99
16/05/2013 jlkjkl 100
11/06/2013 Fritz 0
09/05/2013 Prueba 6
19/06/2013 152
24/05/2013 12
- 0
03/05/2013 0
16/04/2013 12
16/05/2013 34
06/05/2013 9
05/06/2013 Fritz 0
09/05/2013 11 0
20/05/2013 0
09/05/2013 4
09/05/2013 0
10/06/2013 2
17/06/2013 5
14/06/2013 0
14/06/2013 2
14/06/2013 Test 23
12/06/2013 1234556
20/06/2013 Fritz 0

Neat, but does not show you the total amounts. How about we extend our model, so that it would automatically calculate those amounts.

model3
class Model_Purchase_Adding extends Model_Purchase {
  function init(){
    parent::init();

    $pr = $this->leftJoin('product');
    $pr->addField('price')->editable(false); // go edit in Product CRUD

    $this->addExpression('cost')->set('qty * price');
  }
}
$crud=$page->add('CRUD');
$crud->setModel('Purchase_Adding');
if($crud->grid) {
$crud->grid->addButton('Refresh')->js('click',$crud->grid->js()->reload());
}

  
Demo
Date Product Qty Price Cost Edit Delete
13/06/2013 Fritz 5 717.00 3585.00
13/06/2013 Fritz 3 717.00 2151.00
01/05/2013 Fritz 2 717.00 1434.00
01/05/2013 Prueba 99 200.00 19800.00
16/05/2013 jlkjkl 100 8888888.00 888888800.00
11/06/2013 Fritz 0 717.00 0.00
09/05/2013 Prueba 6 200.00 1200.00
19/06/2013 152
24/05/2013 12
- 0
03/05/2013 0
16/04/2013 12
16/05/2013 34
06/05/2013 9
05/06/2013 Fritz 0 717.00 0.00
09/05/2013 11 0 99999999.99 0.00
20/05/2013 0
09/05/2013 4
09/05/2013 0
10/06/2013 2
17/06/2013 5
14/06/2013 0
14/06/2013 2
14/06/2013 Test 23 23.00 529.00
12/06/2013 1234556
20/06/2013 Fritz 0 717.00 0.00

Questions? Post a comment below.

Disqus