This is an outdated version of the Manual. Visit the NEW Manual

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.
To override this convention simply set the REDBEAN_MODEL_PREFIX like this:


    
//dog -> Object_Dog
    
define('REDBEAN_MODEL_PREFIX''Object_');

    
//dog -> \App\Model\Dog
    
define('REDBEAN_MODEL_PREFIX''\App\Model\');

You can also provide your own mapper, here is how:


    $formatter 
= new MyModelFormatter;
    
RedBean_ModelHelper::setModelFormatter($formatter);

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.

PHP Namespaces in Formatter

While you can use the prefix to map beans to namespaced models you can also accomplish this using the mapper:


    
class MyModelFormatter implements RedBean_IModelFormatter {
             public function 
formatModel($model) {
                    return 
'\\'.'Models'.'\\'.$model;
             }
    }
    
$formatter = new MyModelFormatter;
    
RedBean_ModelHelper::setModelFormatter($formatter);

In RedBeanPHP 3.5 the space.php script will append this code (NSModelFormatter) for you to the namespaced file.

For even more complex mapping solutions: In formatModel() use func_get_arg(1) to obtain the bean as well. (since RedBeanPHP 3.1)

Static FUSE

Sometimes you want FUSE to work the other way around. You call a static method on a model and you want to set a bean in the model manually. To accomplish this set the reference to the model as a meta property:


    $this
->bean R::dispense('bean');
    
$this->bean->setMeta('model',$this);

Now, the bean will be connected to the current Model instance.

Scoping rules

In the model, $this->bean refers to the bean. You can access the real bean using $this->bean from inside a model. If a property does not exist $this->$property will return a the property, but it will not return a reference to lists so I recommend to always use $this->bean->$property if you want to access $property of the bean in your model.


 
 

RedBeanPHP Easy ORM for PHP © 2024 Gabor de Mooij and the RedBeanPHP community - Licensed New BSD/GPLv2