php-crud-api icon indicating copy to clipboard operation
php-crud-api copied to clipboard

Filter results if joined table has an authorization.recordHandler

Open chattago2002 opened this issue 2 years ago • 3 comments

Hi. I'm trying to have good results from a call with join and filters.

I have 2 tables: "requests_details" and "requests". For the "requests" table I set the "authorization.recordHandler" middleware to add "filter=user_id,eq,$_SESSION['apiUser']['id']" if the user is not "admin".

The API endpoint is "/records/requests_details?join=requests&filter=request_id,nis".

At the moment I have the following response:

{
    "records": [
        {
            "id": 75,
            "request_id": null,
            "full_name": "John Doe",
            "phone": "00390099888",
            "room": "single"
        },
        {
            "id": 219,
            "request_id": {
                "id": 41,
                "full_name": "Melvin Carr",
                "birthday": "1960-06-27",
                "email": "[email protected]",
                "user_id": 128
            },
            "full_name": "Melvin Carr",
            "phone": "0039444777",
            "room": "double"
        }
    ]
}

For good results I mean those where request_id is not null. I set "authorization.recordHandler" additional as following:

if ($tableName === 'requests_details') { return ($tableName == 'requests_details') ? 'filter=request_id,nis' : ''; } `

Where I'm wrong? Is there another way to achieve the desired result?

Thanks

chattago2002 avatar Jan 18 '24 11:01 chattago2002

Your code:

if ($tableName === 'requests_details') { return ($tableName == 'requests_details') ? 'filter=request_id,nis' : ''; }

Should be:

return ($tableName == 'requests_details') ? 'filter=request_id,nis' : '';

Other than that I see no code problem.

As for the logic: I can see why the filter doesn't work as the null results are caused by the recordHandler. You can apply an customization.afterHandler if you really want to remove those results from the query. You can also list the requests with their corresponding requests_details instead of the other way around.

mevdschee avatar Jan 18 '24 13:01 mevdschee

I was using that code because I have other conditions for other tables but... my code and your code couldn't be considered very similar?

Anyway I replaced my code with yours but results are the same so I decided to use customization.afterHandler but I cannot find information about it and its parameters. I made the following code: if ($tableName == 'requests_details') { $arr = json_decode($response->getBody()->getContents(), true); $arr['records'] = array_filter($arr['records'], function ($obj) { return !is_null($obj['request_id']); }); return ResponseFactory::fromObject(200, $arr, 1); }

Results are ok but the question is: Is this the right way?

Another question: in authorization.recordHandler I can add only the filter parameter? Is not possible to use others as excludeor join? I tried but it seems not working.

chattago2002 avatar Jan 19 '24 15:01 chattago2002

couldn't be considered very similar?

I agree.

Is this the right way?

It seems good to me.

I tried but it seems not working.

I think only filter is allowed.

mevdschee avatar Mar 19 '24 10:03 mevdschee