简体   繁体   English

如何从 Google Cloud Datastore 中查找在 PHP 中具有数值键的实体?

[英]How to look up an entity from Google Cloud Datastore that has numeric value key in PHP?

The following code was run on Google App Engine Flex env.以下代码在 Google App Engine Flex env 上运行。

For example: the key is Key(Task,"t12") .例如:密钥是Key(Task,"t12") I can look it up in PHP using the following syntax:我可以使用以下语法在 PHP 中查找它:

$key = $datastore->key('Task', "t12");
$task = $datastore->lookup($key);

However, if the key is Key(Task, 12) .但是,如果键是Key(Task, 12) I cannot look it up with the syntax:我无法使用以下语法查找它:

$key = $datastore->key('Task', 12);
$task = $datastore->lookup($key);

Or even甚至

$key = $datastore->key('Task', intval("12"));
$task = $datastore->lookup($key);

I tried the query again and it worked well for the first entity (which was created in code).我再次尝试查询,它对第一个实体(在代码中创建)运行良好。 However, for the 3 following entities (which was created using the console) only the last entity worked.但是,对于以下 3 个实体(使用控制台创建),只有最后一个实体有效。 I recognized there a different in the first column.我在第一列中发现了不同之处。

在此处输入图片说明

According to official documentation根据官方文档

Assigning identifiers In addition to a kind, each entity has an identifier, assigned when the entity is created.分配标识符 除了种类之外,每个实体都有一个标识符,在创建实体时分配。 Because it is part of the entity's key, the identifier is associated permanently with the entity and cannot be changed.由于它是实体密钥的一部分,因此标识符与实体永久关联且无法更改。 It can be assigned in either of two ways:它可以通过以下两种方式之一进行分配:

1.Your application can specify its own key name string for the entity. 1.您的应用程序可以为实体指定自己的键名字符串。

2.You can have Firestore in Datastore mode automatically assign the entity an integer numeric ID. 2.您可以让处于 Datastore 模式的 Firestore 自动为实体分配一个整数数字 ID。

3.Assigning your own numeric ID which is integer 3.分配自己的数字ID,它是整数

Therefore I used case 3, and you used case 1. In case 1 you should search for a string because you set a key name when you inserted using console, which is of type string.因此,我使用了案例 3​​,而您使用了案例 1。在案例 1 中,您应该搜索一个字符串,因为您在使用控制台插入时设置了一个键名,它是字符串类型。 When I inserted an entry using client libraries I used my own numeric ID, which is of type int.当我使用客户端库插入一个条目时,我使用了我自己的数字 ID,它是 int 类型。

name=12 and name="12" are two strings which are not the same name=12 和 name="12" 是两个不同的字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM