Suppress relationship beans in findAll() call

32 views
Skip to first unread message

Dennis Riddle

unread,
Jul 15, 2015, 4:45:58 AM7/15/15
to redbe...@googlegroups.com
I'm using RedBean for an app that stores device locations on a map, and I noticed that when I pull a list of maps using findAll(), and then export() the data to an array so that I can jsonEncode it, all of the devices for each map are added to the array. 

As I was using the call to populate a map selection screen, I don't really need all the device data to be returned by this call. Is there currently a way to suppress the loading of owned data when using findAll and export? (Not sure if the data is loading during the findAll or the export).

If not, this would be a useful switch to have.

Dennis Riddle

Steve Dunlop

unread,
Jul 15, 2015, 12:00:47 PM7/15/15
to redbe...@googlegroups.com
Hi Dennis,
I had exactly the same issue with findAll(). Here's how I solved it:

$temp = [];
$scripts
= R::findAll('script');
foreach ($scripts as $script) {
    $temp
[] = $script->export();
}
$result
['scripts'] = $temp;


That only returns the top level of scripts, none of the child elements. If you want to selectively add them, you can 'touch' them within the foreach e.g.

$script->elementcount = $script->countOwn('element');

(element being a child of script)

Hope that helps - or please let me know if you find a better way to do it!

Steve

Dennis Riddle

unread,
Jul 15, 2015, 3:10:07 PM7/15/15
to redbe...@googlegroups.com
My solution was similar:

// Get all map data from database
$maps = R::findAll('map');

// Format as json array
$data = '{}';
if(!empty($maps))
{
     $array = [];
     foreach( $maps as $m )
     {
           $array[] = $m->export( false, false, true );
     }
     $data = json_encode($array);
}

From what I see in the method prototype and code, without that true on the 3rd parameter to export(), it will still export the contents of ->ownDeviceList, which is what I was trying to suppress.

It would be nice to have that parameter accessible from exportAll(), to save code lines if nothing else. 

Also, I'm a big fan of passing optional parameters via arrays or generic objects, in order to avoid ugly call lines like the one above.

Dennis
Reply all
Reply to author
Forward
0 new messages