Adding Actions

Actions are methods which are defined inside a model. Because Agile Toolkit follows the principles of "Object Oriented" design, it properly implements encapsulation inside a single class. Basic methods such as save() and delete() are already implemented by the model parent class, but you are encouraged to add more methods:

class Model_Book extends Model_Table { public $table='book'; function init(){ parent::init(); $this->addField('name'); $this->addField('isbn')->mandatory(true)->caption('ISBN'); $this->hasOne('Publisher'); $this->hasOne('Author'); } function addMultipleBooks($data){ $this->unloadData(); foreach($data as $row){ $this->set($row)->save(); } } }

It's highly suggested that your Business Logic would not rely on the existence of UI or any views.

You can call your actions like this:

$this->add('Model_Book')->addMultipleBooks($data);

Hooks

Agile Toolkit contains a system-wide implementation for "hooks". Models use hooks to allow you to perform actions before or after loading or save operations are taken place. The below example can show how normalization and validation can be implemented in Agile Toolkit.

class Model_Book extends Model_Table { function init(){ parent::init(); $this->addHook('beforeSave,afterLoad',$this); } function beforeSave(){ // manually update some fields before saving. This is to create a indexable field for full-text search $this['book_search_field'] = $this['title'].' '.$this['descr'].' '.$this['author_name']; // let's also perform some validation if(strlen($this['book_name']<10))throw $this->exception('Name of the book is too short'); // normalization will modify field to match some internal rules $this['book_url']=$this['book_url'] ?: preg_replace('/[^a-zA-Z0-9]/','-',trim($this['name'])); } function afterLoad(){ $this['name'] = $this->api->_($this['name']); // wraps name through localization function } }

There are the following hooks available for the model (at least):

Few more recommendation on query use.

Non-relational models

Above hooks also are used for non-relational models, however instead of $query they will receive $id of the record which is about to be loaded or modified.