【问题标题】:Get Entity's ID in Datastore PHP API在 Datastore PHP API 中获取实体的 ID
【发布时间】:2018-03-11 13:16:33
【问题描述】:

有没有办法使用 PHP Datastore API 获取实体的 ID/Key?
我已经对所有内容进行了身份验证,并且能够获取值,但没有 ID

这是我的代码:

<?php
    $title = "Builder | Makeroid Account";
    include "../assets/includes/head.php";

    if (!$USER->is_logged_in()) {
        $USER->redirect('/');
    }

    require_once __DIR__.'/../assets/libs/datastore/vendor/autoload.php';
    use Google\Cloud\Datastore\DatastoreClient;
    use Google\Cloud\Datastore\Query\Query;
    $datastore = new DatastoreClient([
        'projectId' => 'makeroid-builder'
    ]);

    $query = $datastore->query();
    $query->kind('UserData');
    $query->filter('emaillower', '=', $U_DATA['email']);
    $users = $datastore->runQuery($query);

    $params = ["email", "emailFrequency", "emaillower", "isAdmin", "link", "name", "sessionid", "settings", "templatePath", "tosAccepted", "type", "upgradedGCS"];
?>

<div class="content">
    <div class="container-fluid">
        <div class="row">
            <?php foreach ($users as $user) { ?>
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header card-header-icon" data-background-color="rose">
                        <i class="material-icons">assignment</i>
                    </div>
                    <div class="card-content">
                        <h4 class="card-title">User Properties</h4>
                        <div class="table-responsive">
                            <table class="table">
                                <thead class="text-primary">
                                    <th>Key</th>
                                    <th>Value</th>
                                </thead>
                                <tbody>
                                    <?php print_r($user); foreach ($params as $param) { ?>
                                    <tr>
                                        <td><?=$param?></td>
                                        <td><?=$user?></td>
                                    </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <?php } ?>
        </div>
    </div>
</div>

<?php include "../assets/includes/footer.php"; ?>

我想得到这个: 我能够获取行中的所有值,例如我的电子邮件,但我还没有找到获取该 ID 的方法
我尝试过使用$user['id']$user['key']$user['__key__'],但它们都不起作用

【问题讨论】:

    标签: php google-cloud-datastore


    【解决方案1】:

    您需要从实体 (https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.56.0/datastore/entity?method=key) 中提取密钥,然后使用 $key->pathEndIdentifier() 之类的方法从密钥 (https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.56.0/datastore/key) 中提取 id。

    因此,与其尝试将键作为字典值从对象中提取出来,不如尝试将其作为方法提取出来。 $user->key()->pathEndIdentifier() .

    【讨论】:

      猜你喜欢
      • 2021-09-18
      • 2019-08-07
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多