- Getting started
- Basics
- Finding
- Relation Mapping
- Models
- Database
- BeanCan
- Advanced
- Architecture
- Other
How Fuse Works
Fuse adds an event listener (Observer pattern) to the RedBeanPHP object database. If an event occurs it creates an instance of the model that belongs to the bean. It looks for a class with the name Model_X where X is the type of the bean. If such a model exists, it creates an instance of that model and calls loadBean(), passing the bean. This will copy the bean to the internal bean property of the model (defined by the superclass [SimpleModel]). All bean properties will become accessible to $this because FUSE relies on magic getters and setters.
Remapping models
By default RedBeanPHP maps a bean to a model using the Model_X convention where X gets replaced by the type of the bean. You can also provide your own mapper, here is how:
RedBean_ModelHelper::setModelFormatter( new MyModelFormatter );
Here we tell RedBeanPHP to use the MyModelFormatter class to find the correct bean-to-model mapping. This class looks like this:
class MyModelFormatter implements RedBean_IModelFormatter {
public function formatModel($model) {
return $model.'_Object';
}
}
This class will make sure that a bean of type 'coffee' will be mapped to Coffee_Object instead of Model_Coffee.
In the model, $this is bound to the bean. As is $this->bean. You can also access the real bean using $this->bean.
In formatModel() use func_get_arg(1) to obtain the bean as well. (since RedBeanPHP 3.1)
Learn how you can write Models that automatically connect to be beans using FUSE.
Tweet
User contributed notes. Please use the comment section to provide tips, notes and examples. To ask for support or to provide feedback use the forum. For bug reports use Github Issue Tracker.
Site comments powered by Disqus