Using Data Controllers
Model Data Controllers are designed for a simple data storages such as Arrays, BerkleyDB, Memcache, File. For "Model" such a data controller is required for loading and storing the record.
| Data Controller | Table | Notes |
|---|---|---|
| Array | array( ('id'=>'key', 'field'=>'val', 'field'=>'val'), ... ) | Handy for iterating through array of hashes. Supports traversing, Does not support load() |
| ArrayAssoc | array( 'key'=>array('field'=>'val', 'filed2'=>'val'), ... ) | Similar to Array, but uses keys as "id" field. Supports traversing, load() and save() |
| MemCache | key prefix | Allows to store of field data inside memcache. Supports load(), save() and delete(). Does not support traversing. Adds incr() and decr(). |
Using Data Controller
To associate model with data controller you should use setSource($data_ctl, $table=null, $id=null); This will initialize controller, and set $model->controller to point to controller object. calling load(), save() or delete() will be forwarded towards a current controller.
Controller may implement additional methods through addMethod() mechanism, so setting some controllers to your model may make more methods available. Memcache controller adds incr($field), decr($field).
Sample Controller
Here is a sample implementation of a controller, which produces random records.
class Controller_Data_Random extends AbstractController {
function load($model,$id=null){
$res=array();
foreach($model->elements as $name=>$f)if($f instanceof Field){
if($f->system())continue; // do not mess with system fields
if($f->type() == 'int')$res[$name]=rand(0,100);
else $res[$name]='RandomString'.rand(0,100);
}
$model->set($data);
return $model;
}
function setSource(){
}
}
$m=$this->add('Model_User')->setSource('Random')->load(); will now auto-fill fields with random values.
Using with Model_Table
Model_Table redefines save(), load() and delete() to use SQL, however if wish to use it with a custom controller, you can call this:
$m=$this->add('Model_User'); // based on Model_Table
$m->load(25); // Loads from SQL
$c=$this->add('Controller_Data_Random');
$c->load($m); // does not affect ID
$mc=$this->add('Controller_Data_MemCache');
$mc->save($m, 'latest_user'); // saves model into MemCache under id='latest_user'
