Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WhereRaw for Dates in d365 #76

Open
lbarron-md opened this issue Feb 25, 2021 · 4 comments
Open

WhereRaw for Dates in d365 #76

lbarron-md opened this issue Feb 25, 2021 · 4 comments

Comments

@lbarron-md
Copy link

Hi,

Thanks for this amazing library, however I'm having problems trying to filter a query using a date field in D365. They way this accepts is is $filter=<date_field> lt 2021-01-12T14:21:02Z. As you can see the date value is unescaped but the grammar always surrounds the value with single quotes if this is not a boolean.

Is there any workaround or fix for this?

@Joost-Wolthuis
Copy link
Contributor

Anyone has the answer?

@lbarron-md
Copy link
Author

Hey @Joost-Wolthuis, I ended up creating my own dummy client. Notice that is very laravely, but I hope you can figure it out

    public function getEntity(string $entity, array $filters = [], $count = 0)
    {
        $nextLink = null;
        $responseValues = [];
        do{
            $headers  = $this->getDefaultHeaders(); # Gets the auth token

            $endpoint = $this->config->resource . '/data/'. $entity;

            $top          = $count ? '&$top=' . $count : '' ;
            $queryString  = '?$filter=1 eq 1 ';
            $queryString .= implode(' ', $filters);
            $endpoint    .= $queryString . $top;

            // Override if there is a nextLink which means it already got requested
            $endpoint = $nextLink ?? $endpoint;

            $response = Http::withHeaders($headers)->get($endpoint); # You can replace it with guzzle

            $arrayResponse = $response->json();

            array_push($responseValues, ...($arrayResponse['value'] ?? []));

        }while($nextLink = $arrayResponse['@odata.nextLink'] ?? false);

        return $responseValues;
    }
# Usage
        $config        = (object) Config($this->apiConfigPath);
        $client        = new ApiClient($config);

        $zuluStartDate = $this->getStartDate()->toIso8601ZuluString();
        $zuluEndDate   = $this->getEndDate()->toIso8601ZuluString();
        $filters = [
            "and RecordModifiedDateTime ge {$zuluStartDate}",
            "and RecordModifiedDateTime lt {$zuluEndDate}",
        ];

        $payload = $client->getEntity('YourEntity', $filters);

Hope it helps

@KieranFJ
Copy link
Contributor

I implemented internally a 'whereRaw()' method that simply takes a manually built string and adds it as a $filter.
Intended for some edgecases that the library might not have support for immediately.

Might be of use?

KieranFJ@73715f6

Basically you would just need to do something like

$whereRaw = "Quantity add QuantityUsed gt 0";   
$builder->whereRaw($whereRaw); 

If appropriate i could create a pull request.

@ThaDaVos
Copy link

ThaDaVos commented Nov 2, 2021

As they are extending Laravel's Query Builder, why not use the DB::raw function?
I just tried it and it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants