0

I'm trying to query in MongoDB using PHP/CodeIgniter but I can't find a solution...

I have a first query in "function1" where there is

$this->mongo_db->where("category" => getCatID("games");

And in my second function "getCatID" I have

return $this->mongo_db->select('_id')->where("name" => $name)->get('Category');

But it seems that the second function continue on the first query of function1.

I'm using this library...

1 Answer 1

1

I assume your first code snippet was intended to be:

$this->mongo_db->where("category", getCatID("games"));
// or…
$this->mongo_db->where(array("category" => getCatID("games")));

I've not used this library, but from perusing the source code, it looks like all query criteria set by where() is accumulated in an internal $wheres property on the Mongo_db class. This only gets cleared by the private _clear() method, which is called from the following public methods:

Any time either of those terminating methods are called, $wheres will be reset. Likewise, if those methods aren't called, older criteria may bleed into your next criteria. In your case getCatID() may be clearing out earlier criteria. PHP is going to execute getCatID() before invoking the where() method in which it's an argument, so I'd expect that outer where() call to see an empty $wheres array.

One solution would be to call getCatID() on its own before building your second query. Alternatively, you could clone the Mongo_db instance to ensure that each query is built independently.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It is what I did. I create a another MongoDb object and I run this independently...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.