Search code examples
phpzend-frameworkredbean

Integrating RedBean ORM into Zend Framework


The manual for RedBean suggests a method for integrating the ORM into Zend Framework.

From the manual:-

open your Zend bootstrap file and add:

   public function run() {
         $loader = Zend_Loader_Autoloader::getInstance()->registerNamespace("RedBean_");
         require_once( APPLICATION_PATH . "/../library/RedBean/redbean.inc.php"); //or rb.php
         R::setup( "mysql:host=localhost;dbname=timereg", "root" );
         Zend_Registry::set("tools", R::$toolbox);
         Zend_Registry::set("db", R::$adapter);
         Zend_Registry::set("redbean", R::$redbean);
         parent::run();
   }

This method does not strike me as being the most efficient as the ORM is being set up in every controller, whether it is needed or not. It is also using Zend_Registry which I don't like.

There are also certain features of RedBean that need integrating properly and that may benefit from configuration via application.ini:-

How can RedBean ORM be integrated into the Zend Framework in a more efficient Zend like manner?


Solution

  • Depending on what redbean.inc.php does, I don't think you'll be able to improve the efficiency of this too much. The overhead of requiring in a file and setting up a DB connection isn't likely to be significant.

    I'd change the suggested code slightly to:

    protected function _initRedBean()
    {
        $loader = Zend_Loader_Autoloader::getInstance()->registerNamespace("RedBean_");
        require_once APPLICATION_PATH . "/../library/RedBean/redbean.inc.php"; //or rb.php
        R::setup( "mysql:host=localhost;dbname=timereg", "root" );
        Zend_Registry::set("tools", R::$toolbox);
        Zend_Registry::set("db", R::$adapter);
        Zend_Registry::set("redbean", R::$redbean);
    }
    

    to take advantage of the bootstrap's inbuilt resource loading, instead of overriding the run() method (bad practice).

    It might be possible to simply pass in an existing PDO connection if you are also using Zend_Db, to avoid creating a 2nd connection, but that would require some digging around in the code.