- Download RedbeanPHP and drop the two files (._rb.php and rb.php) in system/application/libraries
- Create a library class called Redbean.php with the contents shown below, and drop it in the same directory
- Use it in your controller
Redbean.php
ci =& get_instance(); // Include database configuration include(APPPATH.'/config/database.php'); // Include required files include(APPPATH.'/libraries/rb.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(); } }
Using it somewhere in your controller
// example code of creating a 'post' table $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);
After running this controller, you should see a 'post' table created. Done!
What you should see in phpMyAdmin |