This is an outdated version of the Manual. Visit the NEW Manual


If you are new to RedBeanPHP but you have been working with relational databases in the past you might be a bit confused with the terminology in RedBeanPHP. Here is a little 'cheatsheet' mapping relational terms to RedBeanPHP terms. You might find this useful.

RelationRedBeanPHP method
one-to-one R::loadMulti('author,bio', 1)[1]
one-to-many $author->ownBook[] = $book
many-to-one $book->author = $author
many-to-many $book->sharedCategory[] = $category
one-to-many self-referential$category->ownCategory = $category
many-to-many self-referential$category->sharedCategory = $category
one-to-X $book->fetchAs('author')->coauthor = $coAuthor;
X-to-one $author->alias('coauthor')->ownBook;
many-to-many + properties$book->link('category_book', array('location'=>'2nd floor'))->category = $category[2]
polymorph $eBook = $book->poly('media_type')->media[3]
1This is a very uncommon relation. You can also use One-to-many plus a unique constraint for this.
2You can also rename this association and use access shared lists using via() (3.5+).
3Kind of defeats the purpose of a relational database though because you cannot use foreign keys now.

While RedBeanPHP is perfectly capable of managing almost any kind of relationship, I recommend to keep things simple. Most of the time the basic relations like one-to-many, many-to-one, self-referential relations and many-to-many will do. Sometimes I use one-to-X and X-to-one (aliasing). Personally I never use one-to-one or polymorph, these are extremely uncommon.


RedBeanPHP Easy ORM for PHP © 2019 Gabor de Mooij and the RedBeanPHP community - Licensed New BSD/GPLv2