Search code examples
phpredbean

ReadBeanPHP parameter bindings for several values


Reading the documentation at http://www.redbeanphp.com/querying it gave some examples for using parameter bindings. But I have some questions using it:

Question 1:

Here is the original example

R::getAll( 'SELECT * FROM page WHERE title = :title',
    [':title' => 'home']
);

But what if I want to replace more than one value for search in the SQL statement (for example adding year)? Is it correct to write:

R::getAll( 'SELECT * FROM page WHERE title = :title AND year > :year',
    [':title' => 'home', ':year' => 2012]
);

Question 2:

This example uses question mark ? for replacement

R::getRow( 'SELECT * FROM page WHERE title LIKE ? LIMIT 1',
    [ '%Jazz%' ]
);

If I want to add year, should it be written like this by adding a second question mark in the query?

R::getRow( 'SELECT * FROM page WHERE title LIKE ? AND year > ? LIMIT 1',
    [ '%Jazz%', 2012 ]
);

Question 3:

So far the documentation only gave examples for getAll() and getRow() functions. Will it also work for R::exec()? For example:

R::exec( 'UPDATE page SET title="test" WHERE id = ? AND year > ?',
    [ 101, 2012 ]
);

R::exec( 'UPDATE page SET title="test" WHERE id = :id AND year > :year',
    [ ':id' => 101, ':year' => 2012 ]
);

Thank you!


Solution

  • All your answers are correct.

    If you looked up into rb.php file you can find Facade class and its functions:

    public static function exec( $sql, $bindings = array() )
    
    public static function getAll( $sql, $bindings = array() ) 
    

    Second parameter is array for bindings.