
I assume you have PHP installed, either the command line version (php-cli) or the web version. This quick guide will show you how to use RedBeanPHP in just a few minutes (probably 5 ;) ).
To setup RedBeanPHP for testing purposes, just use:
require('rb.php');
R::setup();
This will create a temporary database in the temporary folder on most systems. If this fails; try passing a valid DSN, username and password (PDO style).
You know CRUD? CRUD stands for CREATE, RETRIEVE, UPDATE and DELETE. RedBeanPHP allows you to perform CRUD operations quite easily. After setting up you don't have to add tables, columns, indexes or foreign keys. All database structures will be generated automatically, on-the-fly, by RedBeanPHP. Just fire-and-forget!
Instead of database records, RedBeanPHP thinks in 'Beans'. A Bean is a plain old PHP object (POPO) with public properties. This is how you create a bean:
$shop = R::dispense('shop');
And this is how you set properties:
$shop->name = 'Antiques';
This is how you store the shop in the database:
$id = R::store($shop);
This is how you load your previously stored shop:
$shop = R::load('shop',$id);
And this is how you delete the shop from the database:
R::trash($shop);
ORM is about mapping relations. Our shop would be quite useless if it does not sell anything, so our shop is filled with $products, those are beans as well. This is how you add a product to your shop:
$product = R::dispense('product');
$product->price = 50;
$shop->ownProduct[] = $product;
R::store($shop);
Simply add products to a list called ownProduct. If you add employees use:
$shop->ownEmployee[] = $mike;
The name of the list should always begin with the word 'own' followed by the name of the type of bean it contains. Remember, the type is the string you pass to the R::dispense() method! So, beans of type 'book' are stored in ownBook, beans of type 'employee' in ownEmployee, beans of type 'box' in ownBox etc. To empty a list:
$shop->ownEmployee = array(); //fires everyone!
You never have to load a list. Lists are fetched as soon as you access the property. This is called lazy loading. You can also mix in some SQL; for instance to sort the products in a list:
$products = $shop
->with(' ORDER BY price ASC ')
->ownProduct; //since 3.3
Learn more about one-to-many relations in RedBeanPHP.
A shared list is just like an own-list but the relationship goes both ways. With an own-list the shop 'owns' the products. They cannot be owned by more than just one shop. With managers, this is not the case; managers can be associated with many shops. So we use a shared list (creates a link table manager_shop):
$shop->sharedManager[] = $jack;
To find beans in the database, we use plain old SQL, for instance the following example will find all shops in the neighbourhood:
$shops = R::find('shop',' distance < 10 ');
Using raw SQL power is possible as well, and just as easy:
R::exec('UPDATE shop SET name = ? ',array('MyShop'));
RedBeanPHP offers methods to retrieve cells,columns,rows and multi dimensional arrays:
R::getCell( ... )
R::getCol( ... )
R::getRow( ... )
R::getAll( ... )
Once you are done developing you need to deploy your PHP application on a production server. Now you don't want RedBeanPHP to scan the database and modify the schema there, so you need to tell RedBeanPHP to freeze:
R::freeze( true );
Simply put that line at the beginning of your application or script before deploying. Right after R::setup( ... ).
Of course there is more to RedBeanPHP than just this, RedBeanPHP allows you to mix PHP and SQL using a fluid query builder, can sync database schemas using just a single command: R::syncSchema(), make deep copies of entire object hierarchies using a single R::dup() and more... much more... However with this tiny introduction you can already start using RedBeanPHP! Have fun!
RedBeanPHP Easy ORM for PHP © 2013 Gabor de Mooij and the RedBeanPHP community - Licensed New BSD/GPLv2