CodeIgniter 2.1 and RedBeanPHP 3.0

Based on a comment on my previous post, I decided to take a look at RedBeanPHP in conjunction with CodeIgniter. In about ten minutes, I had grabbed the latest releases of both (CI 2.1.0 and RedBeans 3.0.2), set them both up and had them playing nice with each other thanks to this great tutorial, and was inserting data into an empty and tableless database.

After following that tutorial, it was really this simple:

$this->load->library('rb');
$taco = R::dispense('taco');
$taco->cost = 14.95;
$taco->title = "Greatest Taco Ever";
$taco->flavor = "Tasty Fake Beef";
$id = R::store($taco);
$taco = R::load('taco', $id);
print_r($taco);

That little chunk of code loaded the RedBeanPHP library in CodeIgniter, built a completely new table in my database, inserted a new ‘taco’, and then retrieved the newly stored ‘taco’ bean and printed its guts out:

RedBean_OODBBean Object
(
    [null:RedBean_OODBBean:private] =>
    [properties:RedBean_OODBBean:private] => Array
        (
            [id] => 13
            [cost] => 14.95
            [title] => Greatest Taco Ever
            [flavor] => Tasty Fake Beef
        )

    [__info:RedBean_OODBBean:private] => Array
        (
            [type] => taco
            [sys.id] => id
            [tainted] =>
        )

    [beanHelper:RedBean_OODBBean:private] => RedBean_BeanHelperFacade Object
        (
        )

    [fetchType:RedBean_OODBBean:private] =>
)

The database table structure it created:

CREATE TABLE IF NOT EXISTS `taco` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `cost` double DEFAULT NULL,
 `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 `flavor` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

So, that’s pretty neat and it’s also the type of thing that can bring developers to tears when they think about how much time they’ve spent dealing with this part of software development. Lots and lots to explore with RedBeanPHP, but I’m already pretty impressed.

This entry was posted in PHP, Web Development and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *