The Power ORM

Data Tools

In RedBeanPHP version 5, some additional functions have been added to increase your productivity even more. These are convience functions building upon the solid foundation of RedBeanPHP, allowing you to quickly perform a lot of work with just a single command.

Look (5+)

To quickly generate template snippets that rely on database data you can use Look:

'SELECT * FROM color 
         WHERE value != ? 
                 ORDER BY value ASC'
'green' ],
'value''name' ],
'<option value="%s">%s</option>''strtoupper'"\n"

Given the colors red, green and blue, this code will return an HTML snippet for a select-list with the colors RED and BLUE in uppercase.

MatchUp (5+)

MatchUp is a powerful productivity boosting method that can replace simple control scripts with a single RedBeanPHP command. Typically, matchUp() is used to replace login scripts, token generation scripts and password reset scripts. The MatchUp method takes a bean type, an SQL query snippet (starting at the WHERE clause), SQL bindings, a pair of task arrays and a bean reference.
If the first 3 parameters match a bean, the first task list will be considered, otherwise the second one will be considered. On consideration, each task list, an array of keys and values will be executed. Every key in the task list should correspond to a bean property while every value can either be an expression to be evaluated or a closure (PHP 5.3+). After applying the task list to the bean it will be stored. If no bean has been found, a new bean will be dispensed. This method will return TRUE if the bean was found and FALSE if not AND there was a NOT-FOUND task list. If no bean was found AND there was also no second task list, NULL will be returned. Here is an example of how we could use MatchUp for a typical password-reset script:

    $newpass '1234';
$didResetPass R::matchUp(
' token = ? AND tokentime > ? ',
        [ [ 
$token \PDO::PARAM_STR ], time()-100 ],
'pass' => $newpass,
'token' => ''
$account );

I recommend to use the PARAM_STR for tokens to avoid casting related security issues.

CSV Queries (5+)

To quickly generate a CSV file with a single RedBeanPHP command use R::csv() like this:

        SELECT city,popularity 
        FROM scores 
        WHERE score > ?
, [5], ['CITY','SCORE'] );

Diff (5+)

To quickly get the difference between two beans (or arrays of beans) and their relations use R::diff(). For instance, let's create a book with some pages:

        list($book,$pages) = R::dispenseAll('book,page*2');
$book->title 'Old Book';
$book->price 999;
$book->ownPageList $pages;
$pages[0]->text 'abc';
$pages[1]->text 'def';

Now we change the book:

        $book->title 'new Book';
$page end($book->ownPageList);
$page->text 'new';

We can now compare both books using:

        $oldBook $book->fresh();
$diff R::diff($oldBook$book);

If we print_r() that variable we'll see:

Array ( [book.1.title] => Array ( [0] => Old Book [1] => new Book ) [book.1.ownPage.2.text] => Array ( [0] => def [1] => new ) )

The 3rd parameter can be used to set a filter, all bean types in the filter will be omitted. To format the keys set a format in the 4th parameter, the default value (giving us this result) is: '%s.%s.%s'. This is printf-like format.

How to use queries

Sometimes using a plain query is more efficient than using beans. For instance, consider the following example:

    $books R::findAll'book' );
$books as $book ) {
$book->sharedCategoryList as $cat ) {

Using a plain query this task could be accomplished far more efficiently:

    $books R::getAll'SELECT 
    book.title AS title, AS author, 
    GROUP_CONCAT( AS categories FROM book
    JOIN author ON = book.author_id
    LEFT JOIN book_category ON book_category.book_id =
    LEFT JOIN category ON book_category.category_id = 
$books as $book ) {

One of the biggest mistakes people make with ORM tools is to try to accomplish everything with objects (or beans). They forget SQL is a very powerful tool as well. Use SQL if you are merely interested in generating reports or lists.

back to main menu

Donate to RedBeanPHP using Monero:
47mmY3AVbRu 7zVVd4bxQnzD
2jR7PQtBJ cF93jWHQ
rP7yRED4qr fqu6G9Q8ZNu7
zqwnB28rz76 w7MaExf
mALVg69yFd 9sUmz
(remove spaces and new lines)

Performance monitor: this page has been generated in 0.02196192741394s. Is the performance lacking? Please drop me an e-mail to notify me!

Partners PapelDigital


RedBeanPHP Easy ORM for PHP © 2024 () and the RedBeanPHP community () - Licensed New BSD/GPLv2 - RedBeanPHP Archives
RedBeanPHP, the power ORM for PHP since 2009.

Privacy Statement