Search code examples
phpredbean

Redbean's Exception Handling not working?


I'm new to RedbeanPHP (v4.0.4), and trying it out a bit.

However, whenever I try to force a mistake (load unexisting bean, store a wrong type value, etc.), it won't throw any exceptions.

It always seems to be successful, whatever mistake I make.

Example:

require APP .'libs/redbean/rb.php';

R::setup($dsn, $user, $pass);
R::freeze(true);

$book = R::load('book', 25);  // unexisting record


R::begin();

try
{
    R::trash($book);
    R::commit();
    echo 'book trashed';  // always shows this
}

catch (Exception $e)
{
    R::rollback();
    echo $e;              // not showing this
}

Any ideas?


Solution

  • 'redbeanphp' is designed to be rather 'flexible'. This means if you use the correct syntax then it is unlikely to fail. However, it does not always do what you expect or wish as regards what you may consider 'errors'. It depends a lot on the 'freeze' setting in 'R::setup'.

    In the the case of 'load' as in:

    $book = R::load('book', 25);  // unexisting record
    

    redbeanphp treats it as a 'R::dispense('book')' statement and returns an empty record if it cannot find the record with that 'id'. The id of the returned 'bean' will be zero.

    In the case of 'invalid columns' or 'values', if you did not freeze the database on the 'R::setup' connection statement then it just creates the column and changes the value.

    If you want to be more strict then 'freeze' the database and/or look at 'FUSE', which allows 'model' classes to be automatically linked to 'beans'. In these 'model' classes you can include a lot of the checks, validation etc.