Search code examples
phpormredbean

redbeanphp hide properties by default


I'm using amazing orm "RedbeanPhp" from http://redbeanphp.com/, I like how it speeds up the development so much, but the documentation is a bit outdated.

  1. How can I select a few properties when using R::findAll()?

For example I only want to select id, email from users, this will grap all the users columns, but I only need 2 columns.

  1. Is it possible to hide certain columns by default inside bean? "hide password inside users for example"?

To solve this I currently use a bit verbose code:

$users = R::findAll('users')->export();
foreach($users as $user){
 unset($user->password, $user->token);//...etc
}
print_r($users);

But it would be nice if could set this inside a fuse model or something?!


Solution

  • You can use getAll($sql_query) instead findAll()

    ...in your example

    $users= R::getAll('select id,email from users')'

    More info about querying with RedBeanPHP here

    Only "con": $users is not an array of beans, just an array of associative array, having colnames as indexes.

    If not enough for you, you can use function convertToBeans(...) as detailed in the link included before