-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Closed
Description
Hi again,
I would like to use the MongoDB connection outside of a Model (ie. a custom class not dependant of a specific collection).
So far:
DB::getMongoDB() does'nt work (dependency issue, couldn't resolve)
DB::connection('mongodb') returns NULL
What is the best practice?
A) still use your IoC ? Then how can I proceed?
B) Open the connection using the original Mongo PHP library, everytime I need it ? Isn't it efficient because it might trigger multiple connections instead of a single instance?
Thanks in advance!
Activity
jenssegers commentedon Oct 23, 2013
What do you mean? Using this library outside of a Laravel project?
nbertal commentedon Oct 23, 2013
Nope, for instance I want to use it in a class that is not a "Model" per say (but inside a L4 project where jenssegers/Laravel-MongoDB is present and configured).
jenssegers commentedon Oct 23, 2013
You should be able to just use the query builder, like you would in Laravel: https://github.com/jenssegers/Laravel-MongoDB#query-builder
nbertal commentedon Oct 23, 2013
That works indeed, but I need to get the MongoClient and MongoDB objects.
nbertal commentedon Oct 23, 2013
At least the MongoDB. This I couldn't succeed outside of a Jenssegers\Mongodb\Model extended class..
jenssegers commentedon Oct 23, 2013
DB::getMongoDB()
should work in that case.https://github.com/jenssegers/Laravel-MongoDB/blob/master/tests/ConnectionTest.php#L27
nbertal commentedon Oct 23, 2013
I suspect it doesn't work because it does not precise what connection to use, so it's using the default one (ie. in my case I use postgre):
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\PostgresConnection' does not have a method 'getMongoDB'
So I thought I was a dependency issue, but I couldn't resolve it.
nbertal commentedon Oct 23, 2013
Just to be clear and summarizing a little:
It seems impossible to access the MongoClient or MongoDB objects when not working with established Models if Mongo not the default DB connection.
nbertal commentedon Oct 23, 2013
I developped my own connection class based on yours, now everything is fine.
Thank you!
waelwalid commentedon Apr 19, 2019
@nbertal
Hi , I have the same issue so could you please tell me what is your solution ?
Thank you :)