saintsystems / odata-client-php

OData Client Library for PHP
MIT License
141 stars 103 forks source link

where clause comparing two columns rather than a column and value. #94

Closed KieranFJ closed 2 years ago

KieranFJ commented 2 years ago

I have been using the library for a while and getting along with it, but have only just now need to filter a column by another column, for example

$filter=QtyOnHand eq QtyReserved

Both columns are decimals.

However the where method treats the value as a string, and eventually puts single quotes around it in Grammar - prepareValue()

so the end result for the URI will have $filter=QtyOnHand eq 'QtyReserved' which ends up with a decimal / string compare error. If I do the same query in postman, minus the quotes, it works fine.

->where('QtyOnHand ', '=' 'QtyReserved');

Doesn't appear to be a way to hint that this is a column and not a value. Am I missing something obvious?

I have made a temp work around (pass a valueType = 'column' or 'value' and eventually decide not to quote the string, and it in theory works.

tavy315 commented 2 years ago

@KieranFJ have you tried you oData API manually and works? It might be not supported by oData, so the library won't be able to do anything about it.

KieranFJ commented 2 years ago

@tavy315 Yeh, if you try something like :

https://services.odata.org/V4/OData/OData.svc/Products?$filter=Rating%20lt%20Price

you'll get relevant results back, but what the library ends up generating is

https://services.odata.org/V4/OData/OData.svc/Products?$filter=Rating%20lt%20'Price'

Which is just looking for the literal string value of 'Price', not comparing the column. Which is due to prepareValue checking the value of and quoting it if its a string.

Example using another REST example

tavy315 commented 2 years ago

@KieranFJ seems that Laravel Eloquent has whereColumn / orWhereColumn methods, but odata-client-php doesn't implement such a feature yet.

KieranFJ commented 2 years ago

Aweseome, thanks @tavy315 Ill updated to the 0.6.0 and pull #95 down and give it a try. Looks a lot better than what i came up with.