如何在php中使用getLastError()来检查我的save方法是否插入到mongo中?
我设置我的数据库如下:
$this->databaseEngine = $app['mongo'];
$this->db = $this->databaseEngine->dbname;
$this->collection = $this->db->collectionname;然后,我的insert查询如下所示:
$query = array(
'fieldname' => $fieldname
);
$this->collection->insert($query);然后,我想使用getLastError()来检查它是否正确插入,如果不正确,原因是什么。但我不确定如何实现它。
我应该在插入后使用:
$this->collection->getLastError("what goes here?");干杯。
更新
我最终使用下面的代码获得了最后一个错误:
echo '<pre>' . print_r($this->databaseEngine->lastError(), true) . '</pre>';Sammaye的方法同样有效,见下文。
发布于 2012-11-28 21:49:30
$this->集合->getLastError(“这里有什么?”);
什么都不会发生,返回的getLastError是来自MongoDB ( http://www.php.net/manual/en/mongodb.lasterror.php )的最后一个错误。它也用于MongoDB类(自动柜员机)。
你不必像那样使用它,相反,你可以这样做:
$this->collection->insert($query, ARRAY('safe' => TRUE));这将从函数返回一个数组,详细说明它是否实际插入。可以通过阅读以下页面找到该数组的详细信息:
http://www.php.net/manual/en/mongocollection.insert.php
https://stackoverflow.com/questions/13605385
复制相似问题