ModelFormatter

From RedBean

Jump to: navigation, search

Formatting Models

In RedBean you can use Fuse to automatically connect Beans to models. Fuse uses a default naming convention to look for models in your code: Model_X. Here X represents the name of the bean (the type, i.e. "page" or "post"). So to connect a bean of type "post" to a model, simply declare a model class with name Model_Post and Fuse will automatically find your model, connect it to the bean and pass methods like R::store() to the model to do some validation. However if you don't agree with this naming scheme, you may write your own. To do this simply create a class implementing the IModelFormatter interface like this:

class mymodelformatter implements RedBean_IModelFormatter{
public function formatModel($model){
return "my_weird_".$model."_model";
}
}

Here we want all our beans to connect to models called my_weird_X_model where X once again represents the type of the bean.

class my_weird_post_model extends RedBean_SimpleModel {
public function blah(){ return "yes!"; }
}

To let RedBeanPHP know we have crafted a new model formatting rule, simply pass it to the modelhelper class:

RedBean_ModelHelper::setModelFormatter(new mymodelformatter);
$w = R::dispense("post");
asrt($w->blah(),"yes!");

This will just work.

Also see: Fuse,Beans,Advanced

Personal tools