Model and Controller Handling

Most of the view objects supplied through Agile Toolkit or through Add-ons are general-purpose. Form for example does not have knowledge about what fields it needs to display to the user. To initialize the form to the set of fields defined in your model, you simply need to associate the form with a model:

$form->setModel('Book');

$m=$form->model// points to object of a class Model_Book

The argument supplied to setModel() can be either an object or a string. If 'Model_' prefix is not present, it will be automatically added. The core AbstractObject will only initialize the model and assign it to a property, but descending objects may (and should) introduce more logic more specific for that particular object.

AbstractView model initialization

The setModel() for view objects is redefined to support a second argument. This argument specifies a list of fields which should be automatically initialized inside that view. Form and Grid are good examples. If you specify second argument "false" then no fields will be initialized automatically.

$form->setModel('Mook',array('name','isdn'));

$f=$form->getElement('isdn');  // points to initialized field object, if it was present in model.

View's default controller

AbstractView property "default_controller" will specify a default controller which will be used for field population. If the property is "null" then fields are not populated. Typically the View will specify this value in it's definition. For example, 'Form' uses 'Controller_MVCForm'.

If the specified controller has method setActualFields and the list of fields were present as second argument, then that method is executed.

View uses setController to initialize default controller primarily due to developer convienence.

setController

This method is designed to initialize controllers with any objects in your system. While Model primarily contains only field and business logic and would not contain any display-related logic, controllers are permitted to be much more diverse in what they are doing. Below are just some application types for the controllers:

Adding Fields

Controller may add additional fields to a model. Using this technique allows you to create standard audit fields:

class Controller_ModelAudit extends AbstractController {
    function 
init(){
        
parent::init();
        if(!
$this->owner instanceof Model)
          throw 
$this->exception('Use with Model only');

        
$this->owner->addField('created_dts');
        
$this->owner->addHook('beforeSave',function($m){
            
$m->set('created_dts',date('Y-m-d H:i:s'));
        });
    }
}

Altering object behaviour

Objects do not have control on how exactly fields are being added into it. To re-order child objects, use "Controller_Ordering". There are many other controllers to look out for.

See also

  • Controller_MVCForm
  • Controller_MVCGrid