Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the connection everywhere #58

Closed
nbertal opened this issue Oct 23, 2013 · 10 comments
Closed

Using the connection everywhere #58

nbertal opened this issue Oct 23, 2013 · 10 comments

Comments

@nbertal
Copy link

nbertal commented Oct 23, 2013

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!

@jenssegers
Copy link
Contributor

What do you mean? Using this library outside of a Laravel project?

@nbertal
Copy link
Author

nbertal commented 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
Copy link
Contributor

You should be able to just use the query builder, like you would in Laravel: https://github.com/jenssegers/Laravel-MongoDB#query-builder

@nbertal
Copy link
Author

nbertal commented Oct 23, 2013

That works indeed, but I need to get the MongoClient and MongoDB objects.

@nbertal
Copy link
Author

nbertal commented Oct 23, 2013

At least the MongoDB. This I couldn't succeed outside of a Jenssegers\Mongodb\Model extended class..

@jenssegers
Copy link
Contributor

DB::getMongoDB() should work in that case.

https://github.com/jenssegers/Laravel-MongoDB/blob/master/tests/ConnectionTest.php#L27

@nbertal
Copy link
Author

nbertal commented 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
Copy link
Author

nbertal commented 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
Copy link
Author

nbertal commented Oct 23, 2013

I developped my own connection class based on yours, now everything is fine.

Thank you!

@waelwalid
Copy link

@nbertal

Hi , I have the same issue so could you please tell me what is your solution ?
Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants