'Custom rows lookup function
@param key: the key field
@param values: the values to look up
@param fields: unused (retained for API compatibility)'

def lookup_rows(self, key, values, fields=[]):

    db = current.db
    s3db = current.s3db
    instance_fields = {'pr_person': ['first_name', 'middle_name', 'last_name']}
    etable = s3db.pr_pentity
    rows = db(key.belongs(values)).select(key, etable.pe_label, etable.instance_type)
    self.queries += 1
    keyname = key.name
    types = {}
    for row in rows:
        instance_type = row.instance_type
        if (instance_type not in types):
            types[instance_type] = {row[keyname]: row}
        else:
            types[instance_type][row[keyname]] = row
    results = []
    append = results.append
    for instance_type in types:
        table = s3db.table(instance_type)
        if (not table):
            continue
        if (instance_type == 'hrm_training_event'):
            training_event_represent = s3db.hrm_TrainingEventRepresent()
            training_event_represent._setup()
            rows = training_event_represent.lookup_rows(table[keyname], values, pe_id=True)
            self.training_event_represent = training_event_represent
        else:
            if (instance_type in instance_fields):
                fields = [table[f] for f in instance_fields[instance_type] if (f in table.fields)]
            elif ('name' in table.fields):
                fields = [table['name']]
            else:
                continue
            fields.insert(0, table[keyname])
            query = table[keyname].belongs(types[instance_type].keys())
            rows = db(query).select(*fields)
        self.queries += 1
        sdata = types[instance_type]
        if (instance_type == 'hrm_training_event'):
            for row in rows:
                append(Row(pr_pentity=sdata[row['hrm_training_event'][keyname]], **{instance_type: row}))
        else:
            for row in rows:
                append(Row(pr_pentity=sdata[row[keyname]], **{instance_type: row}))
    return results
