没有理由$data_object_id
在您调用时应该是 stdClass 的实例__toString()
,除非您在原始帖子中未共享某些代码。如果您粘贴整个脚本来重现您的问题,而不是在它们之间进行讨论的单行,这将更有帮助。
你写了:
echo "Inserted: " . $data_object_id->__toString() . "...";`
我收到以下错误:
Catchable fatal error: Object of class stdClass could not be converted to string`
如果$data_object_id
是 stdClass 实例,则该echo
行将导致以下错误:
Fatal error: Call to undefined method stdClass::__toString()
您应该能够简单地通过检查类型/类$data_object_id
或检查其上是否__toString()
存在该方法来诊断此问题。
考虑以下脚本:
<?php
$m = new MongoClient();
$c = $m->test->foo;
$doc = (object) ['x' => 1];
$c->insert($doc);
printf("Document is: %s\n", get_class($doc));
printf("_id field is: %s\n", get_class($doc->_id));
printf("_id cast to string: %s\n", (string) $doc->_id);
printf("_id toString(): %s\n", $doc->_id->__toString());
echo "\n";
$doc = ['x' => 2];
$c->insert($doc);
printf("Document is: %s\n", gettype($doc));
printf("_id field is: %s\n", get_class($doc['_id']));
printf("_id cast to string: %s\n", (string) $doc['_id']);
printf("_id toString(): %s\n", $doc['_id']->__toString());
这将插入一个单字段文档,测试对象和数组形式,并打印有关_id
在调用insert()
. 这应该会产生以下输出(当然,ObjectId 哈希值会有所不同):
Document is: stdClass
_id field is: MongoId
_id cast to string: 5220ad40e84df1b667000000
_id toString(): 5220ad40e84df1b667000000
Document is: array
_id field is: MongoId
_id cast to string: 5220ad40e84df1b667000001
_id toString(): 5220ad40e84df1b667000001