Richard Holloway Blog

How to use FUSE models in RjhRedbean

Posted in October 2012 by under redbeanphp

With special thanks to Marc Fonteijn for providing feedback, advice and ideas.

For an explanation of what FUSE models are, you will need to read the Redbean Documentation.

The problem with getting this to work in RjhRedbean is that because Zend Framework 2 is using namespaces, that breaks the way FUSE works in getting every bean to find its model automatically.

To fix this we need to :
1. Make sure that the model is not in a named namespace
2. Ensure the class can be found by Zend Framework 2

Using the example given in the Redbean Documentation, we want to create a model for the `band` bean to ensure that we do not have more than 4 members in a band. This example assumes you are working in the Application module.

By convention in Zend Framework 2, you put your source files under modules/Application/src//, so files in the Application\Controller namespace are in modules/Application/src/Application/Controller/. In this case where we want the class "Model_Band" to be in the global namespace, the file will be modules/Application/src/Model_Band.php.

Create the file with the following content

<?php

class Model_Band extends RedBean_SimpleModel
{
  public function update()
  {
    if (count($this->ownBandmember) > 4) {
      throw new Exception('too many!');
    }
  }
}
            

So we now have a FUSE model in the global namespace called Model_Band. FUSE will use this class name when attempting operations on any beans called `band`.

We now need to tell Zend Framework 2 how to find this class. We do this using a class map, which you need to edit manually.

First edit module/Application/Module.php so that the autoloader will load a classmap file. The file should look like this

<?php

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
  public function onBootstrap(MvcEvent $e)
  {
    $e->getApplication()->getServiceManager()->get('translator');
    $eventManager = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
  }

  public function getConfig()
  {
    return include __DIR__ . '/config/module.config.php';
  }

  public function getAutoloaderConfig()
  {
    return array(
      'Zend\Loader\ClassMapAutoloader' => array(
        include __DIR__ . '/autoload_classmap.php',
      ),
      'Zend\Loader\StandardAutoloader' => array(
        'namespaces' => array(
          __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
        ),
      ),
    );
  }
}
            

Finally, we create the classmap to be used by the autoloader containing our new Model_Band class. Edit module/Application/autoload_classmap.php so it look like

<?php

return array(
  'Model_Band' => __DIR__ . '/src/Model_Band.php',
);
            

That should just work. When you try to add a more than 4 members to a bean called "band", the Exception will be thrown.