我需要在Laravel的MongoDB上使用事务。
我下载了php MongoDB驱动程序1.6,然后将php_mongodb.dll复制并粘贴到了php / ext文件夹中。
我还通过以下方式安装了php mongo库
composer require mongodb/mongodb
现在,当我尝试根据this使用事务时,发生错误时它不会回滚。
$client = new Client($url);
$callback = function (\MongoDB\Driver\Session $session) use ($client) {
$data = [
"name" => "Tommy",];
$collection = $client->db1->users;
$user = $collection->updateone(
['mobile' => '*'],['$set' => $data],[$session]
);
$data = [
"activate" => 1,];
$collection = $client->db1->wallets;
$wallet1 = $collection->updateone(
['_id' => 100],['set' => $data],[$session]
);
};
$session = $client->startSession();
$transactionOptions = [
'readConcern' => new \MongoDB\Driver\ReadConcern(\MongoDB\Driver\ReadConcern::LOCAL),'writeConcern' => new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY,1000),'readPreference' => new \MongoDB\Driver\ReadPreference(\MongoDB\Driver\ReadPreference::RP_PRIMARY),];
try{
\MongoDB\with_transaction($session,$callback,$transactionOptions);
return true;
}catch (\Exception $e)
{
return false;
}
laravel中MongoDB\Driver\Session
中的会话类未知,而该类用于mongodb> = 1.4.0。我不知道怎么了谁能帮我吗?