moltin / php-sdk

Community built SDK for using Moltin with PHP
MIT License
47 stars 19 forks source link

Filtering not working #57

Closed jolora closed 7 years ago

jolora commented 7 years ago

Whenever I attempt to use a filter on $moltin->products no results are returned.

->filter([ ['eq' => ['status' => 'live']] ])

The above returns no products but if I try the same filter directly to the API (in postman) it returns products. I've also tried with other attributes like stock but again it does not work when I attempt to do it with the SDK.

I'm using V2.

andrew-waters commented 7 years ago

Hi @jolora - I'm starting work on a slightly different filter system before we merge this in to master so I'll investigate this as part of that work... should be available in a few days...

jolora commented 7 years ago

👍 Thanks @andrew-waters - I look forward to that as I'm keen to get this working next week.

andrew-waters commented 7 years ago

@jolora - do you want to try giving that new branch a try...

the syntax has changed slightly so give it a look over and let me know if there are any issues from there...

jolora commented 7 years ago

Thanks @andrew-waters - unfortunately that's not working for me. Following the first syntax in README is as far as I can tell doing nothing, i.e. whatever filters I apply all products are returned. The later fails totally (and by the way, the readme is missing a closing bracket on that second example).

andrew-waters commented 7 years ago

Could you do me a favour and try and output the URL which is being requested.

You'll be able to output it from the getURL() method here.

jolora commented 7 years ago

Sorry @andrew-waters I'm not sure how. Is it the same URL that I can see in the "links" section of the response?:

Response {#235 ▼
  -statusCode: 200
  -requestID: "f65c7379c643aacf541ef38665a569f8579412f5"
  -executionTime: 0.62158
  -raw: {#324 ▶}
  -data: array:8 [▶]
  -included: {#343 ▶}
  -meta: {#346 ▶}
  -links: {#344 ▼
    +"current": "https://api.moltin.com/v2/products?page[limit]=100&page[offset]=0&sort=price&include=categories"
    +"first": "https://api.moltin.com/v2/products?page[limit]=100&page[offset]=0&sort=price&include=categories"
    +"last": null
  }
  -errors: []
}

EDIT: This is the request I'm making by the way:


$this->moltin->products
->with(['categories'])
->filter([
    ['gt' => ['stock' => 0]]
])
->sort('price')
->all();
andrew-waters commented 7 years ago

Not quite - that's the response wrapper. If you add an echo $this->getURL(); above line 290 that should show us the constructed URL.

Also, if you can add in here the SDK call you're making that would be useful

andrew-waters commented 7 years ago

can you try

$this->moltin->products
->with(['categories'])
->filter([
    'gt' => ['stock' => 0]
])
->sort('price')
->all();
jolora commented 7 years ago

Ok, I've adjusted line 290 of Request.php to look like this:

        echo $this->getURL();
        var_dump($this->getPayload());
        $result = $this->httpClient->request($this->getMethod(), $this->getURL(), $this->getPayload());

Using the syntax I had originally this is the output:

https://api.moltin.com/v2/products
/home/vagrant/Code/drawings/vendor/moltin/php-sdk/src/Request.php:291:
array (size=2)
  'headers' => 
    array (size=6)
      'Authorization' => string '5e579f1d89defa212d4f79a355f47c68a1b02545' (length=40)
      'Content-Type' => string 'application/json' (length=16)
      'Accept' => string 'application/json' (length=16)
      'User-Agent' => string 'moltin-php-sdk/2' (length=16)
      'X-MOLTIN-SDK-LANGUAGE' => string 'php' (length=3)
      'X-MOLTIN-SDK-VERSION' => string 'v2-dev' (length=6)
  'query' => 
    array (size=3)
      'sort' => string 'price' (length=5)
      'filter' => string '' (length=0)
      'include' => string 'categories' (length=10)

Using the latter syntax you just suggested this is the output:

https://api.moltin.com/v2/products
/home/vagrant/Code/drawings/vendor/moltin/php-sdk/src/Request.php:291:
array (size=2)
  'headers' => 
    array (size=6)
      'Authorization' => string '5e579f1d89defa212d4f79a355f47c68a1b02545' (length=40)
      'Content-Type' => string 'application/json' (length=16)
      'Accept' => string 'application/json' (length=16)
      'User-Agent' => string 'moltin-php-sdk/2' (length=16)
      'X-MOLTIN-SDK-LANGUAGE' => string 'php' (length=3)
      'X-MOLTIN-SDK-VERSION' => string 'v2-dev' (length=6)
  'query' => 
    array (size=3)
      'sort' => string 'price' (length=5)
      'filter' => string 'eq(stock,0)' (length=11)
      'include' => string 'categories' (length=10)

Which looks better to my layman's eye. The problem is that my code breaks further down when I attempt to access the included categories: $categories = $response->included()->categories; as I'm told I'm trying to access the property of a non-object.

andrew-waters commented 7 years ago

The second syntax is the correct one (the recently updated one).

What's the full output of var_dump($response) ?

andrew-waters commented 7 years ago

Also was your SDK call to get the second one this:

$this->moltin->products
->with(['categories'])
->filter([
    'gt' => ['stock' => 0]
])
->sort('price')
->all();

The query appears to be using an eq but your previous examples are gt

jolora commented 7 years ago

It seems even with the correct syntax there are always zero products returned if a filter is applied... even if that filter should be returning products. Here's the dump of the response:

object(Moltin\Response)[235]
  private 'statusCode' => int 200
  private 'requestID' => string '43f2a1f69bba2ea50c55dc29e356f3f449c0432c' (length=40)
  private 'executionTime' => float 0.33017
  private 'raw' => 
    object(stdClass)[233]
      public 'data' => 
        array (size=0)
          empty
      public 'meta' => 
        object(stdClass)[229]
          public 'results' => 
            object(stdClass)[219]
              ...
          public 'page' => 
            object(stdClass)[221]
              ...
  private 'data' => 
    array (size=0)
      empty
  private 'included' => null
  private 'meta' => 
    object(stdClass)[229]
      public 'results' => 
        object(stdClass)[219]
          public 'total' => int 0
          public 'all' => int 0
      public 'page' => 
        object(stdClass)[221]
          public 'limit' => int 100
          public 'offset' => int 0
          public 'current' => int 1
          public 'total' => int 0
  private 'links' => 
    array (size=0)
      empty
  private 'errors' => 
    array (size=0)
      empty
andrew-waters commented 7 years ago

Closing this issue as it has been determined there is an issue with mixed filter/sort on the API rather than an issue with the SDK