Below is a full example as described on the Introduction to Models" page of "Learning Agile Toolkit" book
class Model_Item extends Model_Table {
public $table='item';
function init(){
parent::init();
$this->addfield('price');
$this->addfield('name');
$cat = $this->leftJoin('category');
$cat->addField('category_name','name');
$this->hasMany('Order');
$this->hasMany('Order_Completed');
$this->addExpression('successful_orders')
->set($this->refSQL('Order_Completed')
->sum('amount'));
}
}
class Model_Order extends Model_Table {
public $table='order';
function init(){
parent::init();
$this->hasOne('Item');
$this->addField('amount')->type('int');
$this->addField('is_completed')->type('boolean');
}
}
The following object demostrates some of the presentation code. It manipulates existing model values without significantly changing the business logic.
$item = $page->add('Model_Item');
$item->tryLoadAny();
$item['price'] = 14.99;
$item['name'] = 'Mouse Pad '.rand(1,100);
$item->save();
The real power of Agile Toolkit begins when you integrate models with Views.
$grid=$page->add('Grid');
$grid->setModel('Item');
$form = $page->add('Form');
$form->setModel('Order_Completed');
$form->addSubmit();
if($form->isSubmitted()){
$form->update();
$form->js(null,$page->js()->reload())->univ()->successMessage('Order added')->execute();
}