Using get_records with files

I am trying to return a specific set of files by ids using get_records. The code works fine for 'Item' but not 'File'.

$items = get_records('Item', array('range'=>'38,9,35'), $limit);

works fine while

$files = get_records('File', array('range'=>'38,9,35'), $limit);

returns the number of files set by $limit no matter what values are in range. I was expecting that get_records would behave the same for both items and files.

I suspect that the difference is that the range you are passing in the second parameter is different. Items and Files are stored in separate tables, so the range of IDs for items won't necessarily correspond to the the range of IDs for files. So, passing the same range for Item and File aren't going to get the same results.

If I'm following right, you'll probably want to query the range of Items, then for each $item do $files = $item->getFiles(); to dig up the actual files.

Then again, if you tell a little more about the final outcome you are shooting for, there might be another approach available.

Thanks for the Sunday response, Patrick. Well the user wants to specify the images appearing on a page regardless of the item the image file belongs to. In addition, the user wants to display description, creator and date for each file which I can retrieve from the item but would rather retrieve from the file. And while my example used the same ids for the files and for the items, I would be using specific ids for files not ids for items. So the problem is that even when I use ids for files (and actually the values 39,9,35 are file ids) I get returned the entire list of all files with the total number of files displayed limited by the value in $limit. get_records('File', array('range'=>'38,9,35')) doesn't work but using Item does.

I dug through a little more, and it looks like the same range option for Items isn't implemented for Files. That means that it is ignored, and so you get all the results up to the $limit.

So, the most straightforward option might be to start with the Item and dig up the File from there.

Another option (that I haven't tested yet) might be to create a function that basically implements that functionality. I think it would look something like:

<?php
function files_by_range(string $range)
{
$table = get_db()->getTable('File');
$select = $table->getSelect();
$table->filterByRange($select, $range);
$files = $table->fetchObjects($select);
return $files;
}

?>

I had hoped that wasn't the case but I had pretty well decided that get_records for files didn't support ranges and thought I would need to create a function to do that for me. Thanks for the framework on the function. If I do something good with this, I'll post it back.

Will

I found a simpler way. I am using this in a shortcode so I have something like [concarousel ids=38,9,35] where 38,9,35 are the ids for files (not items). So I needed to loop over the file ids. By making the list of ids into an array, I was able to do that.

$ids = explode(',',$args['ids']);
foreach ($ids as $key => $file){
$files[$key] = get_record_by_id('File', $file);
};

Then much as the carousel shortcode does I pass $files on to code that loops through the files and builds the html. The shortcode creates a connected, double carousel of square thumbnails and full sized images so as you select the thumbnail the full sized image advances in its carousel. Once I finish this shortcode plugin, I'll put it up on github.

Fantastic. Thanks!

I recently added the report plug in. I installed it successfully and was able to create a report. However, the there is an error when ever I click on the "generate report". How do I fix this problem?

This topic doesn't deal with the report plug in so I am assuming you meant to post elsewhere.

I have a shortcode plugin that implements a connected carousel, thumbnails below for navigation and the fullsize image above in a carousel that slides to the image when you select a thumbnail. The plugin also implements the code fragment outlined above for using files instead of items to retrieve the images. It is on github at https://github.com/wgcowan/concarousel.