Jump to content

Recommended Posts

Hello all!

I have a couple questions regarding designing a backend with PHP and Mongo. 

How should I handle connections and database edits/queries/etc...?  I see three possible options:

 

Should I make a database and collection pointer a variable for each Object?

Or

Should I make those when certain functions are called by the Object?

Or

Should I keep all database pointers separate from my class structure?

 

I have two main Objects: a Host and an Event.  A Host is basically, a member.  An Event is created and owned by the Host.  I need the Host to be able to Login and add/edit their Events.

Link to comment
https://forums.phpfreaks.com/topic/261738-oo-php-and-mongodb-design-questions/
Share on other sites

Hello,

 

I'm not so sure how familiar you are with MongoDB but at a first glance I'd structure my mongo documents (or objects) so that I have hosts document and within that I'd have events. Event "documents" inside Host documents. It all comes down to what other needs you may have. But as simple application as you are describing this works great.

 

I've worked with PHPs MongoDB library and my personal opinion is that it works great and doesn't really need to be wrapped too much. But I sometimes miss things like unified syntax for saving an object into a database.

- Keep your mongo connections separated (if you have many connections)

- Keep your mongo database instances separated too in a container, create them when needed

- Keep collections under your databases so you can initialize new objects (in PHP)

- One object in PHP = One document in MongoDB Collection

 

This way when object is saved in PHP (or fetched), the object itself will know what collection it belongs to, collection knows what database to use, and db knows what connection to use.

 

Here are resources for mongodb and PHP: http://www.mongodb.org/display/DOCS/PHP+Libraries,+Frameworks,+and+Tools - I've looked into them and some of them seems useful enough to use. In my case I decided to wrap my own classes to help me with mongo :)

 

Just to check something out: http://crodas.org/activemongo.php - This is what I mean. Allthough that library seems very useful I don't like the API of it. But that is the path I'd take.

Thanks for the reply!

There doesn't seem to be too much documentation of structuring your MongoDB connections out there.  I see a lot of different ways, but no real 'Structure' of how it should be done.

I ended up making a connection and a pointer to their collection in the __construct() function.  That way, when I create a Host, or Event, they have their own connection directly to their Collection as opposed to opening them up through the code that uses the Objects. 

I did also create functions that utilize the adding of new Objects in my class that use the insert() function, simply for the fact that I need that assign their MongoID back as another variable and re-save after the insert() function creates an ID for them.  Kinda sucks do to double entry, but I only have to do it on new User registration and then the relationship between Hosts and Events is set forever for later recalling.

I made my Host and Events separate Objects, because a Host can have seven Events, and each Event has two array variables within it.  Embedding each Event within the Host would be Hell with all the nesting of arrays.  Also, on the user end, they will be searching for Events instead of Hosts so each Event needs a different ID.

Thanks for the reference to the articles/blogs.  Helpful!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.