Beans

From RedBean

Jump to: navigation, search

Contents

Beans

RedBean is a lightweight object relational mapping tool. The central concept of RedBean is the bean. A Bean is a simple object that acts as a data container. RedBean has two modes of operation: fluid and frozen. By default RedBean operates in fluid mode. This means that you can just store a bean using RedBean and it will adapt the database schemas if needed. If you are done developing you can freeze() RedBean and deploy to production environments.This is the idea: In fluid mode (default), if you throw a bean at RedBean it will store that bean for you, no matter what. That sounds easy doesnt it?

Getting Started

To start developing with RedBean, the easiest way is to use the kickstart method. (The R-facade is even easier but also somewhat limited). A (Kickstarter) helps you to get started quickly without any prior initialization.

$toolbox = RedBean_Setup::kickstartDev( "mysql:
      host=localhost;dbname=oodb","root","" );

The KickstartDev is meant for the development phase (after the development phase you should use kickstartFrozen). It accepts three arguments; a database connection string (DSN), a username to access the database and finally a password (may be an empty string like in the example). Make sure you catch the output returned by this function; it consists of a very handy toolbox that you can use along the way. First make sure you get an instance of the RedBean Core instance from the toolbox.

$redbean = $toolbox->getRedBean();

If you do not want RedBean to alter the database structure anymore; you can either use:

$redbean->freeze();

or another kickstart method:

$toolbox = RedBean_Setup::kickstartFrozen( $dsn, $user, $pass );


Beans

RedBean simulates an object oriented database. The goal of RedBean is pretty simple; you give it an object and it stores the object for you. The objects you exchange with RedBean are not models but just plain beans. A bean can have many properties that are all public. A bean also has a type. To create a bean of a certain type you must ask RedBean to dispense such a bean for you:

$post = $redbean->dispense("post");

Now we have a post bean and we can populate this bean with all kinds of properties.

Rules for properties

There are some limitations concerning properties. First, the __info property is reserved for all meta information concerning the bean (like its type). Also the id property represents the primary key. A property may contain only primitive values.

Store a Bean in the database

To store a bean in the database we simply fill it:

$post->title = "My First Post";
$post->created = time();

And then we hand it over to RedBean to store it for us:

$id = $redbean->store( $post );

Now the beauty of RedBean is that it will create a post table for us automatically on the fly, as well as three columns (id, title and created) each with an appropriate column type and it will store the record for us in a readable way. It will also return the insert id. Of course you can customize the way beans look like in the database (see prefixes).

Loading a Bean

Retrieving a bean from the database is even simpler. For instance, if we want to load the previously saved bean, we simply ask for it like this:

$post = $redbean->load("post",$id);

Now, we have got our (blog)post object back. There is not much to tell about this is there? We simply ask for a post with id $id and we get what we ask for.

Updating a Bean

Another beautiful feature of RedBean is that it seamlessly updates the database structure if needed; for instance if we decide to introduce a completely new property:

$post->rating = 5;
$redbean->store( $post );

RedBean just alters the table to make place for the new property by adding a column of the correct type.

We may even change the value from a column to a different type; of course in objects we do not really notice this, but RedBean will widen the column for us if we need more space:

$post->rating = "3 Stars";
$redbean->store( $post );

Deleting a Bean

To delete a bean from the database:

$redbean->trash( $post );

Importing and Exporting

RedBean has two service methods to facilitate ultra-fast bean loading. To import values from an array (i.e. POST):

$mybean->import($_POST,"intro,body,id"); 

This does the same as:

$mybean->intro = $_POST["intro"];
$mybean->body = $_POST["body"];
$mybean->id = $_POST["id"];

To export a bean to an array for use in a View object:

$aDullArray = $mybean->export();

Tainted

Sometimes its useful to know whether a bean has been modified or not. The current state of the bean is stored in a Meta property called *tainted*. To get the state of the bean use:

$bean->getMeta("tainted");

If the bean has been modified this will return boolean TRUE, otherwise it will return FALSE.

Also see: Meta,Toolbox

Fuse

Beans are not models (Anemic Domain). A bean is simply a data container whereas a model contains domain specific logic. RedBean allows you to do 3 types of ORM mapping:

  • Use Beans only (good for prototypes).
  • Build models that use beans.
  • Use Beans only and add models that are auto-discovered using Fuse

The third option allows your application to grow. You can first prototype your app. by using beans; resulting in fast developemnt cycles. Then, if you are refining your architecture you can add models using the FUSE name convention. Fuse will automatically connect the beans to their associated models. This approach allows you to create an evolving application; from beans to models without refactoring your code.

Personal tools