airtable-php icon indicating copy to clipboard operation
airtable-php copied to clipboard

Keep manual reorder on fields links

Open KillianLeroux opened this issue 5 years ago • 2 comments

Hello, it's me (again :) ).

I have a new problem:

My records have several fields with type "Link to another record", and I would like keep the manual reorder when I retrieve them, but this isn't the case today :'(

The manual reorder explanation: https://support.airtable.com/hc/en-us/articles/217521267

I started to research where the problem comes from and I viewed that related_field are retrieved by a new API call with a filterByFormula on RECORD_ID() in OR, example:

OR(RECORD_ID() = 'recdpDDhXpoZOhvny', RECORD_ID() = 'recNwuDQa3evOLZnm')

The full API call: https://api.airtable.com/v0/app75XXX/Team%20Members?filterByFormula=OR%28RECORD_ID%28%29+%3D+%27recdpDDhXpoZOhvny%27%2C+RECORD_ID%28%29+%3D+%27recNwuDQa3evOLZnm%27%29

So the sort order is lost here, because the API return records without considering the sort order in parent table :/

KillianLeroux avatar Sep 15 '20 07:09 KillianLeroux

So I found a solution, I wanted create a pull request but no permissions :'(

I add this code in Response.php after while( $relation_request = $relation_response->next() ); in line 162:

// Keep order
if (count($relation_ids) > 1) {
    $related_records_sorted = array();

    foreach ($relation_ids as $relation_id) {
        $key = array_search($relation_id, array_column($related_records, 'id'));

        if ($key !== false) {
            $related_records_sorted[] = $related_records[$key];
        }
    }

    $related_records = $related_records_sorted;
}

Can you push this code if it's ok for you please? =p

KillianLeroux avatar Sep 17 '20 08:09 KillianLeroux

FYI I updated the code to remove the unset.

KillianLeroux avatar Sep 17 '20 10:09 KillianLeroux