If you have come to this page you are probably eager to get started, frustrated or dissapointed because of earlier experiences with ORM. You do not want to read anything, you just want to get things done this time. Well, we can do that. On this page we will learn you how to use RedBeanPHP to quickfix your ORM right now. If you are satisfied you can continue and read the rest of the manual if you like.
Now let's get started. Start your local apache server and phpmyadmin. Create a database called
oodb
with username
root
and no password (this is default in most development server distributions). After Downloading RedbeanPHP, open the RedBean folder and create a new file.
For now, just start redbean by including the example file that ships with RedBeanPHP.
require("example.php");
RedBean stores objects called beans. This is how you create a bean:
$book = $redbean->dispense("book");
And this is how to store it:
//Store a Bean $book->author = "Santa Claus"; $book->title = "Secrets of Christmas"; $id = $redbean->store( $book );
All tables are created on the fly.
This is how you (re)load a bean from the database:
//Load a Bean $aBookFromSanta = $redbean->load( "book", $id ); echo $aBookFromSanta->title;
To associate beans:
//Relations
$library = $redbean->dispense("library");
$library->name="Santas Library";
$redbean->store($library);
assoc($book, $library);
To delete a Bean:
$redbean->trash( $book );
Note that this causes the connection between book and library to break automatically.
All code:
include("example.php");
//Create a Bean
$book = $redbean->dispense("book");
//Store a Bean
$book->author = "Santa Claus";
$book->title = "Secrets of Christmas";
$id = $redbean->store( $book );
//Load a Bean
$aBookFromSanta = $redbean->load( "book", $id );
echo $aBookFromSanta->title;
//Relations
$library = $redbean->dispense("library");
$library->name="Santas Library";
$redbean->store($library);
assoc($book, $library);
//Trash a Bean
$redbean->trash( $book );
That was Quick wasn't it?
This 1-2-3 Tutorial is just a very brief introduction to RedBean. It is not a complete description of what RedBean is and what it is capable of. RedBean is 100% pure object oriented code, without configuration. RedBean supports Caching, optimizing, locking / journaling, trees, various relational structures. It even supports MySQL strict mode. But to learn more about these features you should consult the rest of the manual.