Question

how do i use getLastError() within php to check if my save method is inserting to mongo?

i set up my db as follows:

$this->databaseEngine = $app['mongo'];
$this->db = $this->databaseEngine->dbname;
$this->collection = $this->db->collectionname;

my insert query then looks like this:

$query = array(
    'fieldname' => $fieldname
);
$this->collection->insert($query);

I want to then use getLastError() to check whether it is inserting correctly, and if not why. but im not sure how to implement it.

do i use after the insert:

$this->collection->getLastError("what goes here?");

cheers.

Update

i eventually used this to get the last error:

echo '<pre>' . print_r($this->databaseEngine->lastError(), true) . '</pre>';

Sammaye's way works just as well, see below.

Était-ce utile?

La solution

$this->collection->getLastError("what goes here?");

Nothing goes there, the return of the getLastError is the last error from MongoDB ( http://www.php.net/manual/en/mongodb.lasterror.php ). Also it is used on the MongoDB class (atm).

You don't have to use it like that, instead you can do:

$this->collection->insert($query, ARRAY('safe' => TRUE));

This will return an array from the function detailing whether or not it actually inserted. The details of the array can be found by reading this page:

http://www.php.net/manual/en/mongocollection.insert.php

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top