Tutorial

From RedBean

Jump to: navigation, search

Contents

Learning RedBean

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");
$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 ).

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

If you are done developing and there is no need for RedBean to create anymore tables or modify table structures you can boost the performance of your application significantly by freezing RedBean:

R::freeze(); //no more schema changes!

Deleting A Bean

To remove a bean from the database use:

R::trash( $book );

This will remove the previously stored book.

Learn more about Beans

Association

Beans can be associated with eachother. Associating beans with RedBean is easy. For instance, if we have a scheduling system we can associate employees with a certain room like this:

$employee1 = R::dispense("employee");
$employee2 = R::dispense("employee");
$room = R::dispense("room");
$room->name = "R1";
$employee1->name = "Bill";
$employee2->name = "Steve";
R::associate( $room, $employee1 );
R::associate( $room, $employee2 );

To find out who is inside the meeting room...

$employees = R::related( $room, "employee" );

To kick Steve out of the room:

R::unassociate( $room, $employee2 );

To put Steve in another room:

 R::associate( $room2, $employee2 );

To kick everyone out of the meeting room:

R::clearRelations( $room, "employee" );

Read more about Associations.

Finding Beans

Finding Beans with RedBean is simple. To search for a bean with RedBeanPHP use the Finder. The Finder accepts plain old SQL and converts the results to beans. For instance if we want to get all the employees older than 60:

$seniors = R::find("employee", " age > 60 "); 
foreach($seniors as $senior) {
  echo $senior->name;
}

And here is another example:

$beans = R::find("book",
"title=? and author=? ",
array("Christine","Stephen King"));

To find just one bean use:

R::findOne("book", .. );


'Note 'You can also use ORDER BY and LIMIT statements in your SQL.


Learn more about Finding Beans and Gold SQL...

Trees

RedBeanPHP also supports trees. Here is an example of a tree in RedBeanPHP:

R::attach($father,$daughter);
R::attach($father,$son);
$children = R::children( $father );
$father = R::getParent( $son );

Copy

To copy a bean and its associations use the 'copy' method:

$copy = R::copy( $bean, "nuts,sauces");

The second argument contains a comma separated list of associated bean types that need to be copied.


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.


Querying

Sometimes you want to use your database just as a database. Relational database have powerful features for generating summaries, reports and statistics. There is simply no need to count hundreds of beans if you can run a query. To make use of the SQL power of RedBean just use one of these methods:

R::getAll("SELECT * FROM book ");
R::getRow("SELECT * FROM book WHERE id = 2 ");
R::getCol("SELECT title FROM book ");
R::getCell("SELECT 123 ");

Learn more about Querying...


Debugging your code

Need to see all the SQL that RedBeanPHP generates ? You can easily switch on the internal RedBeanPHP debugging system. Analyze your SQL using the following command:

R::debug( true );

After activating the debugger all SQL statements that are processed by the driver will be printed on the screen.


Installing RedBean with GIT

The full version of RedBean PHP can be installed using GIT. RedBean uses GIT for deployment and versioning. To checkout the latest RedBean full edition:

git clone http://github.com/gabordemooij/redbean.git

A list of all commands

Here is a list containing all simple commands List of R-Commands.

Read further: next...

Personal tools