Queries
From RedBean
Queries
As detailed in the previous chapters, RedBean is not an ORM layer that denies the existence of SQL. On the contrary, RedBean allows and encourages you to write SQL because SQL is considered to be both powerful and elegant. You should not try to replace SQL with OO based techniques (see Finding Beans); if you need SQL, use SQL.
When should I use SQL instead of Beans
This question is hard to answer because it depends on many factors and especially the context of your code. There are however some rules of thumb I use to determine whether I need to use an SQL based approach instead of an OOP based approach. I personally tend to use plain old SQL over objects if:
- An OOP equivalent of the required SQL becomes too complex or too slow
- The problem at hand is a 'typical' database problem.
Of course the decision is up to you and this text is merely an opinion. However this also explains why RedBean lacks certain features and more importantly; why it does not try to mimic SQL with some sort of object oriented pseudo language.
How to fire Queries in RedBean?
The R-facade allows you to perform most of the queries you need. For more advanced uses see: Querying and QueryBuilder.
To execute an SQL query:
R::exec( "update page set title='test' where id=1" );
To fetch a multidimensional resultset directly after firing the query:
R::getAll( "select * from page" );
Note that you can use bindings as well:
R::getAll( "select * from page where
title = :title",
array("title"=>"home") );
or:
R::getAll( "select * from page where
title = ?",
array("home") );
To fetch a single row:
R::getRow("select * from page limit 1");
To fetch a single column:
R::getCol("select title from page");
To fetch a single cell:
R::getCell("select title from page limit 1");
RedBean also offers a very powerful variation of the Unit_of_Work pattern.

