When it comes to PHP-based ORM systems, the usual suspect or most popular choice is Doctrine, but as in any programming endeavor there are many ways to skin the cat, and one of the alternatives I found online is RedBean; the project's website highlight the virtues and merits of this ORM like ease of use, implementation and prototyping while being fast and lightweight (I wonder if it be in kilograms or pounds).
The major gripe I have against RedBean is the lack of resources on how to use it. Call me lazy but I found the RedBean documentation and tutorial messy. First, the RedBean website has a download link that downloads just one file (rb.php), while there's another RedBean ORM version from the same author with more files from GitHub. Second, when you search for RedBean resources and tutorials online, they give you information in bits and pieces that doesn't make much sense all by itself, you have to search again for other resources that will support or rectify your previous reference. Third and lastly, depending on where you downloaded RedBean from, to include and invoke RedBean, it's either you declare:
require("/path/to/RedBean/rb.php");
or:
require("/path/to/RedBean/redbean.inc.php");
In this tutorial we presume you know how and where to put the files in your preferred web server (Apache is the usual choice, though nobody's stopping you from using Nginx, Lighttpd, or go crazy with IIS) and that you have the proper access credentials to a database server that RedBean can and will connect to (in this case, it's MySQL, you can also try PostgreSQL or SQLite). The database connection parameters for my setup as I write this are as follows:
Database type: mysql (for MySQL and would be pgsql if it's PostgreSQL or sqlite, obviously for SQLite)
Hostname: localhost (this may instead be the network address of your database server)
Database name: rbtest (this is the name of the database file you've already created)
Username: root (for more secure configuration, it shouldn't be like this)
Password: password (you're database access password should be more secure than this)
With those parameters we'll formulate the data source name (DSN) and pass it as parameter to RedBean to a variable $toolbox, like so:
$toolbox = RedBean_Setup::kickstartDev("mysql:host=localhost;dbname=rbtest", "root", "password");
We get the RedBean object (indicated by $r) that will manage the interaction with the backend database like below:
$r = $toolbox->getRedBean();
Using the newly generated RedBean object $r, we can create a table complete with table columns and content:
$person = $r->dispense("person");
$person->title = "Saint";
$person->firstName = "George";
$person->lastName = "Estregan";
$person->currentLocation = "Makati City Philippines";
$person->homeLocation = "Taguig City Philippines";
To write the data into the backend database, we execute this command:
$id = $r->store($person);
From this point onward, you can use the resources found on the RedBean site, I leave as exercise for you how to implement the whole CRUD modus operandi using RedBean, it should be easier to figure out now.
UPDATE:
As of 1 August 2011, RedBean 2.0 was released, the documentation is more robust, and worth checking out, so better open a new browser tab and go to http://www.redbeanphp.com.
From this point onward, you can use the resources found on the RedBean site, I leave as exercise for you how to implement the whole CRUD modus operandi using RedBean, it should be easier to figure out now.
UPDATE:
As of 1 August 2011, RedBean 2.0 was released, the documentation is more robust, and worth checking out, so better open a new browser tab and go to http://www.redbeanphp.com.
No comments:
Post a Comment