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).
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 by RedBeanPHP.
Instead of database records, RedBeanPHP thinks in 'Beans'. A bean always has a type and an id. The type matches the name of the table where the bean is stored. 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 before deploying. Right after R::setup( ... ).
RedBeanPHP provides a lot of easy-to-use static methods. If you don't like this you can use the same methods on the objects behind the facade as well. Learn how to use the RedBeanPHP core objects.
RedBeanPHP Easy ORM for PHP © 2024 Gabor de Mooij and the RedBeanPHP community - Licensed New BSD/GPLv2