RedBean + CodeIgniter, Take 2

So I recently wanted to use the newest version of RedBean ORM (1.3beta) with the latest CodeIgniter Reactor version. The whole integration process is even simpler than before, so here goes:

  1. Download RedBean, copy “rb.php” to “application/third_party/rb”
  2. Create “application/libraries/rb.php”, and put this inside:
    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    class Rb {
    
    	function __construct() {
    		// Include database configuration
    		include(APPPATH.'/config/database.php');
    
    		// Get Redbean
    		include(APPPATH.'/third_party/rb/rb.php');
    
    		// Database data
    		$host = $db[$active_group]['hostname'];
    		$user = $db[$active_group]['username'];
    		$pass = $db[$active_group]['password'];
    		$db = $db[$active_group]['database'];
    
    		// Setup DB connection
    		R::setup("mysql:host=$host;dbname=$db", $user, $pass);
    	} //end __contruct()
    } //end Rb

  3. Load the library like any other
    $this->load->library('rb');

  4. Do fun stuff with the beans in your controller:
    $album = R::dispense('album');
    $album->title = '13 Songs';
    $album->artist = 'Fugazi';
    $album->year = 1990;
    $album->rating = 5;
    $id = R::store($album);

For more info and examples just go to the RedBean docs.

And yeah, Fugazi rocks!

Integration of RedBean ORM with CodeIgniter

Updated version is up. Check it out!

I still haven’t updated my work, so here’s something in the meantime.

I’ve managed to successfuly integrate RedBean into the CodeIgniter PHP framework. I don’t think any additional explanation is necesary since it’s pretty simple and basic stuff.

Here are the reuired steps:

  1. Download RedBean and put it into /app/thirdparty/redbean/
  2. Create a library (/app/libraries/redbean.php). The class shoud be called Redbean, and the __construct() function should look something like this:
    $this->ci =& get_instance();
    
    // Include database configuration
    include(APPPATH.'/config/database.php');
    
    // Include required files
    include(APPPATH.'/thirdparty/redbean/redbean.inc.php');
    
    // Database data
    $hostname     = $db[$active_group]['hostname'];
    $username     = $db[$active_group]['username'];
    $password     = $db[$active_group]['password'];
    $database     = $db[$active_group]['database'];
    
    // Create RedBean instance
    $toolbox = RedBean_Setup::kickstartDev('mysql:host='.$hostname.';dbname='.$database, $username, $password);
    $this->ci->rb = $toolbox->getRedBean();
  3. Your controllers can then do some fun stuff like this:
    $post = $this->rb->dispense("post");
    $post->title = 'My first post from CodeIgniter';
    $post->body ='Lorem ipsum dolor sit amet, consectetur adipisicing elit....';
    $post->created = time();
    $id = $this->rb->store($post);

Haven’t tested it much but everything seems to work.
If someone has a better sollution just let me know.