Search code examples
databaseperldatabase-designdbix-class

DBIC Prefetch with mulitple entries works but throws an hash error, is there a way to fix this?


I’m trying to speed up my application and I’ve been playing with prefetch in DBIC prefetching.

I have the following relationships

  • Device has a Many to Many relationship with an Object ( there is a relationship helper called ‘device_has_objects’ )

  • A Object Belongs to a Network.

I’m trying to minimise the amount of calls to the DB. The following seems to work

$self—>db->ResultSet(‘Object’)->search({}, 
          { prefetch => { device_has_objects => 'device' , 'network' }}  
          ); 

But I get this error,

‘Odd number of elements in anonymous hash’

Although the SQL it generates looks ok.

SELECT me.objectid, me.description, me.objectname, me.objecttype, me.network_networkid,         network.networkid, network.network  
device_has_objects.device_deviceid, device_has_objects.object_objectid, device.devicename,   device.devicetype, device.deviceid 
FROM Object me 
LEFT JOIN network network ON network.networkid = me.network_networkid 
LEFT JOIN Device_has_Object device_has_objects ON device_has_objects.object_objectid = me.objectid 
LEFT JOIN Device device ON device.deviceid = device_has_objects.device_deviceid;

The purpose of the search is to return objects which belong to certain networks whilst also providing the device/s that the object exists on.

Is there a way to avoid this error?


Solution

  • ok I feel silly,

    I had to pass and array to prefetch

    prefetch => [ { device_has_objects => 'device' } , 'network' ]
    

    this works fine and doesn't through an error.