看看以下数据存储脚本:
dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
}
在此脚本中,使用突变的提交在开发服务器上不起作用。他们既不保存数据也不抛出任何错误。尽管它们在生产服务器上按预期工作。不推荐使用的突变在开发服务器和生产服务器上均按预期工作。我进行了调试,发现使用google \ appengine \ runtime \ RealApiProxy的<?php
use google\appengine\datastore\v4\Mutation\Operation;
use google\appengine\datastore\v4\BeginTransactionRequest;
use google\appengine\datastore\v4\BeginTransactionResponse;
use google\appengine\datastore\v4\CommitRequest;
use google\appengine\datastore\v4\CommitRequest\Mode;
use google\appengine\datastore\v4\CommitResponse;
use google\appengine\datastore\v4\RollbackRequest;
use google\appengine\datastore\v4\RollbackResponse;
use google\appengine\runtime\ApiProxy;
use google\appengine\runtime\ApplicationError;
function beginTransaction(){
global $request, $response;
$request = new BeginTransactionRequest();
$request->setCrossGroup(true);
$response = new BeginTransactionResponse();
try{
ApiProxy::makeSyncCall('datastore_v4', 'BeginTransaction', $request, $response, 60);
}
catch(ApplicationError $e){
echo $e->getMessage();
exit();
}
$request = new CommitRequest();
$request->setTransaction($response->transaction);
$request->setMode(Mode::TRANSACTIONAL);
}
function prepareEntity($entity, $name, $author, $channel){
global $projectId;
$entity->mutableKey()->mutablePartitionId()->setDatasetId($projectId);
$entity->mutableKey()->addPathElement()->setKind('Books');
$entity->addProperty()->setName('Name')->mutableValue()->setStringValue($name);
$entity->addProperty()->setName('Author')->mutableValue()->setStringValue($author);
$entity->addProperty()->setName('Channel')->mutableValue()->setStringValue($channel);
}
function commit(){
global $request, $response;
$response = new CommitResponse();
try{
ApiProxy::makeSyncCall('datastore_v4', 'Commit', $request, $response, 60);
}
catch(ApplicationError $e){
echo $e->getMessage();
exit();
}
}
$projectId = $_SERVER['APPLICATION_ID'];
beginTransaction();
prepareEntity(
$request->addMutation()->setOp(Operation::INSERT)->mutableEntity(),
'Contemporary Abstract Algebra',
'J Gallian',
'Mutation'
);
prepareEntity(
$request->addMutation()->setOp(Operation::INSERT)->mutableEntity(),
'Principals of Mathematical Analysis',
'Rudin',
'Mutation'
);
commit('Mutation Commit');
echo "<h2>Mutation Commit</h2>";
foreach($response->getMutationResultList() as $mutationResult){
echo 'Book@'. end($mutationResult->getKey()->getPathElementList())->getId(). '<br/>';
}
echo '<hr/>';
beginTransaction();
prepareEntity(
$request->mutableDeprecatedMutation()->addInsertAutOId(),
'Contemporary Abstract Algebra',
'J Gallian',
'Deprecated Mutation'
);
prepareEntity(
$request->mutableDeprecatedMutation()->addInsertAutOId(),
'Principals of Mathematical Analysis',
'Rudin',
'Deprecated Mutation'
);
commit();
echo "<h2>Deprecated Mutation Commit</h2>";
foreach($response->getDeprecatedMutationResult()->getInsertAutoIdKeyList() as $key){
echo 'Book@'. end($key->getPathElementList())->getId(). '<br/>';
}
echo '<hr/>';
函数调用无法产生响应。有人可以解释这种现象。那是开发运行时中的错误吗?
答案 0 :(得分:0)
我希望您的问题出在use google\appengine\datastore\v4\...
行,您可能应该使用google-cloud-datastore库。入门指南位于https://cloud.google.com/php/getting-started/using-cloud-datastore。