在Datastore PHP API中获取实体的ID

时间:2018-03-11 13:16:33

标签: php google-cloud-datastore

有没有办法使用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"; ?>

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

1 个答案:

答案 0 :(得分:3)

您需要从实体(https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.56.0/datastore/entity?method=key)中提取密钥,然后使用类似$ key-&gt; pathEndIdentifier()的内容从密钥(https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.56.0/datastore/key)中提取ID。

因此,不要试图将对象作为字典值从对象中拉出,而是尝试将其作为方法拉出来。 $ user-&gt; key() - &gt; pathEndIdentifier()。