Tutorial
From RedBean
Contents |
Learning RedBean
To get started with RedBeanPHP 1.1 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 following command:
R::setup();
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".
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 (MySQL and PostgreSQL).
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
Referencing other beans
Sometimes beans should be connected. For instance a book can have an author. While an author may have written many books in our example a book can have just one author. In this case we simply link the book to the author like this:
$book = R::dispense( "book" ); $author = R::dispense( "author" ); $book->title = "a Book"; $author->name = "Me"; R::link( $book, $author ); R::store( $book );
Given a book we can now retrieve the author as follows:
$author = R::getBean( $book, "author" );
To break the connection between a book and its author use:
R::breakLink( $book, "author" );
Many-to-many associations
Imagine a scheduling system. The system contains both employees and meeting rooms. To book a meeting room the system associates employees and rooms. This means that a room can be associated with multiple employees but an employee may have booked several rooms. Therefore this is a N-M relationship. To create an N-M like association use:
$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 get all the employees currently in the meeting room:
$employees = R::related( $room, "employee" );
Now Bill and Steve reside in the same room. However Steve may have another appointment in about an hour in another room:
R::associate( $room2, $employee2 );
At some point he thus has to exit the first room. To break an N-M association use:
R::unassociate( $room, $employee2 );
To clear all associations for a give bean type:
R::clearRelations( $room, "employee" );
This command will kick every employee out of room 1.
Learn more about Associations...
Finding Beans
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 = Finder::where("employee", " age > 60 ");
And here is another example:
$beans = R::find("book",
"title=? and author=? ",
array("Christine","Stephen King"));
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 );
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::$adapter->getDatabase()->setDebugMode(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

