CRUD

From RedBean

Jump to: navigation, search

Contents

Setting up

To get started with RedBeanPHP first Download the all-in-one pack from this website and include the php file in your php code like this:

require("rb.php");

Now setup RedBean using the one of the following commands:


SQLite on Linux, Mac, BSD

R::setup();  

SQLite platform independent

R::setup("sqlite:database.txt"); 

MySQL

R::setup("mysql:host=localhost;dbname=oodb",$user,$pass);

PostgreSQL

R::setup("pgsql:host=localhost dbname=oodb",$user,$pass);

By default RedBeanPHP uses SQLite as it's storage engine. If you like to use MySQL or another database OR if you are using Windows, pass an appropriate PDO DSN.

Create a Bean

RedBean works with Beans. The idea is pretty simple. You create a bean using the dispense() function. A Bean is nothing more than a plain object with public properties. You fill the properties with values and pass it to RedBean to store it in the database. RedBean takes care of the rest. It creates tables, columns and determines the data type. A Bean always has a type. The type of a bean determines the name of the table.

$book = R::dispense("book"); //list($b1,$b2) = dispense("book",2); for more books
$book->title = "MyBook";
$id = R::store($book);

This will store a bean of type "book". In the database you will notice a new table called book, containing one record having a field "title" with value "book". (You can also customize the schema: Prefixes,Custom_Keys ). If you are done developing you should freeze the database schema to prevent further changes before you deploy to a production environment:

R::freeze( true );

Loading A Bean

To load a bean from the database use:

$book = R::load( "book", $id );

This will load the previously saved book. We can now adjust its properties:

$book->name = "Yet Another Title, I changed my mind.";

And we can add even new properties on the fly:

$book->rating = 4;
$book->available = true;
R::store($book);

RedBeanPHP will add the new properties for you to the table automatically. Also RedBeanPHP will deterine the required data types for your properties.

TIP! Instead of setting each property individually you can automatically fill a bean with a $_POST array using:

$bean->import($_GET);
$bean->import($_POST, "name,year"); //only these fields

To export a bean to an array use:

$bean->export();
To export a collection of beans (since 2.0) :
R::exportAll( $beans );

Deleting A Bean

To remove a bean from the database use:

R::trash( $book );

This will remove the previously stored book. To delete all books in the entire database we use wipe():

R::wipe("book"); //gone...


Swap

In many cases you need to swap just a single value. For instance if you have an ordered list you often have an icon depicting an arrow that moves an item up, increasing its importance, priority or ordering number. In this case you want to swap the priorities of the selected item and the item preceding the selected item. To accomplish this in RedBeanPHP you can use the swap() method:

$tasks = R::batch("task",array(1,2));
R::swap($tasks,"priority"); //swaps priorities of task 1 and 2.

Count

To count the number of beans of a certain type:

$books = R::count("book");
Personal tools