2015-01-26 87 views
0

我尝试了很多技巧来获取mongodb _id的值,但所有这些都会导致错误。我想这些:无法获得mongodb _id值在php

$new_id = new MongoId(); 
print_r($new_id['_id']); // -> Internal Server Error 
echo $new_id['_id']->{'$id'}; // -> Internal Server Error 
print_r($new_id); // no error, but useless 

所以,我不知道怎么做是正确的

+0

检查你的服务器错误日志 – khartnett 2015-01-26 19:41:49

+0

“致命错误:不能使用MongoId类型的对象作为数组...” – Jacobian 2015-01-26 19:44:11

回答

1

此代码的工作对我来说:

$mongoId = new \MongoId(); 
$strId = $mongoId->__toString(); 

print_r($strId); die; 

MongoId::__toString — Returns a hexidecimal representation of this id

public string MongoId::__toString (void)

http://php.net/manual/en/mongoid.tostring.php

请检查您的服务器错误否则记录。

+0

我做了ctrl-c + ctrl-v你的代码,仍然有这个错误:“致命错误:无法使用MongoId类型的对象作为数组...“。 – Jacobian 2015-01-26 19:47:57

+0

你说得对,对不起伙伴。我纠正了我的答案。测试了Windows和Linux。工作正常。 – mate64 2015-01-26 19:56:20

+0

现在,这是绝对正确的!谢谢! – Jacobian 2015-01-26 20:41:18