Using RedBeanPHP ORM with Zend Framework
UPDATE: If you are using Zend Framework 2, I have written a ZF2 module that integrates RedBeanPHP ORM with ZF2.
This post will demonstrate how simple it is to use RedBeanPHP ORM with Zend Framework 1.
It assumes that you have a LAMP stack set up on a modern Linux system.
Lets go.
1. Download Zend Framework
In order to get the latest version of Zend Framework, download the latest stable release from zend.com.
As of writing the direct link is: http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.tar.gz
2. Set up Zend Tool
Extract the archive and ensure bin/zf.sh file somewhere in your path.
The simpliest way to do this is to create a bin/ directory in your home directory and link to the zf.sh file. Open a terminal and type:
richardjh@ubuntu:~$ cd ~/bin/
richardjh@ubuntu:~$ ln -sf /path/to/ZendFramework-1.11.11/bin/zf.sh zf
You can test this is set up and working by opening a terminal and typing:
richardjh@ubuntu:~$ zf show version
You should see something like:
richardjh@ubuntu:~$ zf show version
Zend Framework Version: 1.11.11
3. Create a Zend Framework project
Create a new project using zf tool:
richardjh@ubuntu:~$ zf create project /var/www/example
Copy the library/Zend folder from the extracted archive to /var/www/example/library/
You can test this is set up correctly by visiting http://localhost/example/public/ in your web browser.
You should see the welcome screen:

4. Download and "install" RedBean
The download is available from the RedBean website by clicking on the big green "DOWNLOAD NOW" button. The direct link is http://redbeanphp.com/downloadredbean.php
RedBeanPHP makes it really easy by providing a version where all the code is in just one file. That file is rb.php.
Create a new directory /var/www/example/library/RedBean/
Extract the RedBean archive and copy the rb.php file from the extracted archive to /var/www/example/library/RedBean/rb.php
5. Integrate RedBean with Zend Framework
We will need a database, I'm using sqlite so will need somewhere to store the database file.
Create a new directory /var/www/example/data/db/
Make sure the web server can write to the directory.
The easiest way is to allow full read/write access to the directory which is fine on your local machine for testing.
Now we need to tell Zend Framework to include RedBean, we can do this in the Bootstrap, so open /var/www/example/application/Bootstrap.php and add a function:
public function _initDatabase()
{
require_once( APPLICATION_PATH . "/../library/RedBean/rb.php" );
R::setup( "sqlite://" . APPLICATION_PATH . "/../data/db/application.db" );
}
And that is it. We can now use ReadBean within Zend Framework.
Notice that we haven't actually created a database. Not even the file. RedBean will take care of this for us.
6. Build a really simplified example
First we build our model. For this example we will just set up adding a record.
Create the model using Zend Tool:
richardjh@ubuntu:~$ cd /var/www/example/
richardjh@ubuntu:/var/www/example$ zf create model Item
Edit the new file /var/www/example/application/models/Item.php and add a function addOne() that looks like this:
public function addOne( $text )
{
$item = R::dispense( 'item' );
$item->text = $text;
$item->added = date('Y-m-d h:i:s');
$id = R::store( $item );
return $id;
}
Now create an action to add an item:
richardjh@ubuntu:/var/www/example$ zf create action add Index
Edit the file /var/www/example/application/controllers/IndexController.php such that the addAction() looks like:
public function addAction()
{
$itemAdapter = new Application_Model_Item();
$id = $itemAdapter->addOne( 'Example list item' );
$this->view->id = $id;
}
And in order that we can get some visibility of it working, edit the view in /var/www/example/application/views/scripts/index/add.phtml so it looks like:
7. Test it works
Visit http://localhost/example/public/index/add/ in a web browser as you should see:
Added a record with new id 1
8. Review
Without writing hardly any code or creating any tables or even creating a database it appears we have just inserted a record.
First, notice that a file /var/www/example/data/db/application.db has been created. This is a sqlite database.
We can see what has happened by using the sqlite3 command:
richardjh@ubuntu:~$ sqlite3 /var/www/example/data/db/application.db
At the sqlite> prompt type .schema to get the database schema.
sqlite> .schema
CREATE TABLE `item` ( id INTEGER PRIMARY KEY AUTOINCREMENT , `text` TEXT, `added`
NUMERIC);
You can see we have a table called item. Now see what records we have:
sqlite> SELECT * FROM item;
1|Example list item|2012-04-19 09:32:56
RedBeanPHP has created the database for us, created a table called item with reasonable fields and then inserted the record into this table.
9. Read the documentation
You can read the manual at http://redbeanphp.com/manual/. I have also created a cheat sheet which you can download in PDF format from the Cheatography website.
If you have any questions or suggestions, please feel free to send me an email, or post a comment below.
10. Download the code from github.
The result of following this guide has been put together as a "zf1-redbeanphp" project on github.
ABOUT THE AUTHOR
Richard Holloway is a PHP developer and System Administrator based in West Sussex