Using RedBeanPHP is easy. First create a bean. A bean is just a plain old object with public properties. Every bean has an id and a type.
$book = R::dispense('book');
Now we have an empty bean of type book with id 0. Let's add some properties:
$book->title = 'Gifted Programmers';
$book->author = 'Charles Xavier';
Or… if you're in the mood for chaining you can use:
$book->setAttr('title','Gifted Programmers')->setAttr(..) etc…
(requires version 3.3+)
Let's store this bean in the database:
$id = R::store($book);
That's all? Yes. Everything has been written to the database! RedBeanPHP automatically creates the table and the columns.
Note that the store() function returns the ID of the record. Also, there is storeAll($beans) in RedBeanPHP 3.1
RedBean dynamically adds columns if you add new properties:
$book->price = 100;
RedBeanPHP also updates the column type to support a different type of value. For instance, this will cause the column to change from TINYINT to DOUBLE:
$book->price = 99.99;
You can store other data types as well:
$meeting->when = '19:00:00'; //Time
$meeting->when = '1995-12-05'; //Date
$photo->created = '1995-12-05 19:00:00'; //Date time
$meeting->place = '(1,2)'; //only works in postgres
$meeting->place = R::$f->geomFromText('"POINT(1 2)"');
You can use R::isoDate() and R::isoDateTime() to generate the current date(time) if you like.
To dispense multiple beans at once:
$twoBooks = R::dispense('book',2);
Bean types may only consist of lowercase alphanumeric symbols a-z and 0-9
A property name has to begin with a letter and may consists of letters and numbers. Underscores
in property names
are allowed but I recommend to come up with better nouns instead.
Underscores are not allowed because RedBeanPHP uses these to identify relations. Underscores are used to denote a relation between two beans, therefore you should not dispense such beans yourself.
The RedBeanPHP naming restrictions allow RedBeanPHP to figure out relationships among tables and beans without configuration; however these rules also help you to maintain a clean and consistent database schema. Moreover, developers often make up terrible names for tables (i.e. 'tbl_userrights', 'person_Project' etc…). I try to encourage people to take some time to find the correct 'name' for their beans (i.e. 'privilege','participant'). This also improves the readability (and maintainability) of your database. Just take some time to find the noun that describes the entity you're modelling best.
If, for some reason you really need to break these rules use: Use R::setStrictTyping(false);
RedBeanPHP Easy ORM for PHP © 2013 Gabor de Mooij and the RedBeanPHP community - Licensed New BSD/GPLv2