phpgoogle-app-engineapp-engine-ndb

order by not working in Google App Engine php


I have used Google App Enginein PHP with ndb

Now I have to save data a model and fetch from that

$query = $datastore->query()
    ->kind('warehouse'); 

$result = $datastore->runQuery($query);

warehouse has name and address fields

and this query is working well

when I try this

use Google\Cloud\Datastore\Query\Query;

$query = $datastore->query()
    ->kind('warehouse')->order('created', Query::ORDER_DESCENDING);
$result = $datastore->runQuery($query);

This is not working, Name has StringProperty. how can I get data with order desc from ndb

https://cloud.google.com/datastore/docs/concepts/queries


Solution

  • Create a index.yaml file and put this code in this

    indexes:
    - kind: warehouse
      ancestor: no
      properties:
      - name: created
        direction: desc
    

    if you want to apply filter

     $query = $datastore->query()
     ->kind('warehouse')->filter->('address'=>'USA')->order('created', Query::ORDER_DESCENDING);
    

    then in index.yaml

    indexes:
    - kind: warehouse
      ancestor: no
      properties:
      - name: address 
      - name: created
        direction: desc
    

    and upload it

    gcloud datastore indexes create index.yaml