I save car details to Google Datastore. Each car detail has a key. Example: OPEL-ADAM
Into the entity I save the car details for my cars. Each car detail has a version number saved to. If something will be changed, I add a new entity into datastore with the ID of the (Example: OPEL-ADAM) car and a new version number. If I have a OPEL-ADAM into the datastore, and if I changed the details 2 times, my last OPEL-ADAM record has version 3. I save a timestamp ("created") everytimes too.
I want to get now the last record, I insert for OPEL-ADAM.
How can I do it with php and the php library google/cloud-datastore ?
Thanks for help!
In case you have the following structure:
OPEL-ADAM: [version:1, timestamp:..]
OPEL-ADAM: [version:2, timestamp:..]
...
Try the following code:
$query = $datastore->query()
->kind('car')
-> where('id','=','OPEL-ADAM')
->order('timestamp', 'DESCENDING')
->limit(1);
I suppose the when you add a new entity with a new version, the version is greater than the previous one, I mean to say every time the last record has a higher version. So you can query by 'timestamp' as above or by 'version'.
$query = $datastore->query()
->kind('car')
-> where('id','=','OPEL-ADAM')
->order('version', 'DESCENDING')
->limit(1);