PHP 8.4.0 RC4 available for testing

MongoDB\BSON\ObjectId::__toString

(mongodb >=1.0.0)

MongoDB\BSON\ObjectId::__toStringRetorna a representação hexidecimal deste ObjectId

Descrição

final public MongoDB\BSON\ObjectId::__toString(): string

Parâmetros

Esta função não possui parâmetros.

Valor Retornado

Retorna a representação hexidecimal deste ObjectId.

Exemplos

Exemplo #1 Exemplo de MongoDB\BSON\ObjectId::__toString()

<?php

var_dump
((string) new MongoDB\BSON\ObjectId());
var_dump((string) new MongoDB\BSON\ObjectId('000000000000000000000001'));

?>

O exemplo acima produzirá algo semelhante a:

string(24) "56731b49da14d8747d701211"
string(24) "000000000000000000000001"
adicione uma nota

Notas Enviadas por Usuários (em inglês) 1 note

up
-1
Jared C.
8 years ago
I found I had to manually call the __toString method when I was trying to populate an associative array of mongoguid => stringValue:

$filter = [];
$options = [];
$this->propertyMap = [];
try {
$query = new \MongoDB\Driver\Query($filter, $options);
$cursor = $this->mongo->executeQuery('MyDbName.MyColectionName', $query);
} catch (Exception $e) {
echo "Mongo Query failure pulling the collection" . PHP_EOL;
echo($e->getMessage());
die("aborting");
}
foreach ($cursor as $property) {
//$this->propertyMap[$property->{_id}->__toString()] = $property->name;
$this->propertyMap[$property->name] = $property->{_id}->__toString();
}

You could also do it as $property->{_id} . "" to get the same string result. Otherwise you end up with an array of MongoDB\BSON\ObjectID objects.
To Top