Parse error: syntax error, unexpected 'class' (T_CLASS) in /var/www/agiletoolkit.org/_/codepad/atk4-addons/documenting/lib/View/Example.php(60) : eval()'d code on line 2

Parse error: syntax error, unexpected 'class' (T_CLASS) in /var/www/agiletoolkit.org/_/codepad/atk4-addons/documenting/lib/View/Example.php(60) : eval()'d code on line 2

Parse error: syntax error, unexpected '$crud' (T_VARIABLE) in /var/www/agiletoolkit.org/_/codepad/atk4-addons/documenting/lib/View/Example.php(60) : eval()'d code on line 2

Parse error: syntax error, unexpected '$crud' (T_VARIABLE) in /var/www/agiletoolkit.org/_/codepad/atk4-addons/documenting/lib/View/Example.php(60) : eval()'d code on line 2

Parse error: syntax error, unexpected 'class' (T_CLASS) in /var/www/agiletoolkit.org/_/codepad/atk4-addons/documenting/lib/View/Example.php(60) : eval()'d code on line 2
Agile Toolkit

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

SOURCE
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
SOURCE
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:

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

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

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

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

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

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

SOURCE
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
The example was executed successfully, but did not produce any output

Questions? Post a comment below.