DataMapper ORM

Row Index Finder (rowindex)

To enable these methods, add 'rowindex' to DataMapper's config, under 'extensions'.

This extension allows you to determine on which row a specific item is found across a non-LIMITed query.

This can be very useful for switching to the correct page of a long list of results after adding or editing a new item.

row_index($id, $leave_select, $distinct_on)

The $leave_select and $distinct_on arguments are used to assist in complex queries.

This method will clone your object, so you can use the existing query to load the content.


$widgets = new Widget();

// user sorting
$widgets->order_by($this->session->userdata('widget_sort_column'), $this->session->userdata('widget_sort_dir'));

$edited_item = $this->session->userdata('last_edited_widget');

// which row?
$index = $widgets->row_index($edited_item);

// now load that page (you could redirect instead)
$widgets->get_paged($index, $this->session->userdata('widget_page_size'), TRUE);

row_indices($ids, $leave_select, $distinct_on)

A simple variation of the above, but this can be used to find multiple IDs in one pass.

The results are an array are associative. The key is the row number, and the value is the row's ID. This allows for non-distinct queries.


$widgets = new Widget();
$indices = $widgets->row_indices(array(1, 3, 22));
// echos something like:
// row -- ID
    5  => 3,
    14 => 1,
    16 => 22


How it Works

This extension uses some fairly complex methods to try and generate the simplest query needed to get the index of every item in proper order. It does this by eliminating all unnecessary SELECTS.

Then the result set is looped over, and the ID or IDS are looked for. If any are found, they are returned.

It's not super efficient, but there aren't any cross-DB patterns that I know of to do this.