I am attempting to integrate Zend with MongoDB. I think I am almost there, but I need some help to complete the task. I am new to both zend and mongo, but reasonably experienced with PHP and MySQL.
Some details:
My Zend project is based on the skeleton tutorial at http://framework.zend.com/manual/2.0/en/user-guide/skeleton-application.html. This works fine on my computer, but is mysql-based.
I have installed MongoDB. I can write nosql on the command line and I can instantiate a MongoClient object in a PHP script, so those things seem to work and talk to each other.
I want to use mongo instead of mysql, and I have implemented Doctrine. I have no idea what Doctrine does, and I've been through several searches to try and find how to run mongo commands in zend. These facts lead me to conclude that I don't know what I'm doing.
So to the specifics of my question:
As I understand it, I should be instantiating a mongo object somewhere. If so, where should I put the instantiation in zend?
If not, presumably I need an existing instantiation of the mongo object to run various find/save commands from inside the model class, so if that's true, how do I access the existing mongo object?
Thanks for any help, and clarification questions are equally welcome.
Edited 13/12/2012 for clarification.
With the given answers it seems I am on the right track, and I can further clarify what I need:
Looking at the Doctrine Documentation linked below, I am still unclear where the document manager instantiation is supposed to occur. I would have expected it to go into a more generic location which is run on every page load. Also, most of the tutorials I find seem to refer to a file structure different given from the skeleton application, so when they say to put certain things in certain places, I'm not sure where they mean. For example, I keep seeing references to bootstrap files, but my only bootstrap files are in test directories.
My questions are these:
What code do I need to add into the indexAction function (which definitely responds when I go to the correct URL) if I simply want to print_r all the documents in a collection?
Do I need to add any other code elsewhere, if so where, and what code?
These are the different bits and what they do, working from the bottom up:
mongodb: data persistance
doctrineODM: Object Document Mapper. This maps mongo documents to php objects. It means you don't have to worry about doing any calls against mongo directly - you just work with plain old php objects. To use doctrineODM you need to define your model classes, and get the hang of using the DocumentManager - suggested, the Doctrine docs are quite good.
doctrineMongoODMModule: A module that integrates doctrine with zf2. The main thing it does is allow you to configure doctrine using zf2 config style, and gives you access to the DocumentManager via the zf2 ServiceManager. (I does quite a few other things too, but they are the main ones)
zf2 (mvc): Lets you define an interface so other things (like a web client) can manipulate your documents.
There is brilliant module doctrine-mongo-odm-module for mongoDB access.
Just add to composer.json
"require": {
....
"doctrine/doctrine-mongo-odm-module": "dev-master",
....
Doctrine documentation should help how to use it.
Related
After many fruitless searches I have decided to put my head above the parapet. I am building an EF6 code first application for an embedded device.
I have successfully followed many references to enable me to create SQL CE files in the folder locations I need.
However, I need to have two separate models running at the same time
1 - Admin model deals with common things like user logins and generic details.
2 - Project specific model where the real work gets done.
The concept is that the Admin database file resides and remains on the
device, but the project database file can be passed from one device to
another.
Is it possible to have two different model databases open at the same time in EF6?
If so is there a good reference site that my search terms have not yet found that will offer advice appropriate to my use-case?
Thanks folks.
I have been able to solve my own question now having found a new article to refer to Multiple Model EF 6 Data points
Read the article on how to solve my question and other variants of similar problems. The key to solving this problem was to specify separate folders for each context migration when commanding the NuGet Package Manager Console. Then in the initialiser for each context, refer to the configuration in the appropriate migration folder. Rather than repeating here, please see the article on MSDN I linked to as all the information I needed was in there, plus lots more.
Im in the start up phase of creating an internal system based on PHP and MongoDB. The users of this system are Javascript programmers and would like to be able to make custom queries to the Mongo database from a frontend gui with arbitrary Mongo shell queries. Of course this would not be a problem at all if I forced them to to write the queries with proper PHP arrays etc, but i would definitely like to avoid this.
I am not quite sure how to approach a feature like this without writing some advanced methods being able to restructure the queries to proper formated arrays that can be used in MongoClient PHP. One approach would be making use of the i.e. MongoDB::execute() method and run the javascript on the database server - a method i don't fancy at all.
Im kindly asking if you have any ideas on how to achieve the requested functionalities to some extend.
Thank you in advance.
Are you looking for something like this : http://rockmongo.com/ ?
I was looking at trying to generate a form based on a Mongoose schema definition. I was having trouble finding where the schema info is tucked away. Where is the path type info etc kept in the object?
Or better, has anyone tried to do this already? I'm using Jade but something that pumps out HTML would also be good.
My little project creates complete CRUD for a mongoose schema. Its a little rough but
might be useful.
https://github.com/jspears/bobamo
It is very client side though, all the forms are generated (they can be overriden with static versions if it is not what you need). That is it doesn't use jade, but it does use underscore template on the client and jqtpl on the server to generate the javascript that makes the form.
Wow that was really unclear... The browser talks to the server via JSON/REST it loads this into an all javascript front end. This front end is generated on the node server on demand, to the client. So it easy to modify, and relatively cleanly sepeartes the data from the view.
I can recommend checking out Formage (npm: formage)
https://github.com/Empeeric/formage
You can get good ideas from this project.
If you're exporting out your models, ie
module.exports = mongoose.model("ModelName", Model)
Check in your module's .schema. That should have all the info you need.
I am new to Zend and still learning.
I need to read some configuration data from the database and keep it in session. I want this data in all the pages, so I need to intercept all the request and check if the session contains the required data, if not I will fetch it from the database and put it in the session.
I would like to know the best place to put the code in. In Java we used to check that in a filter. I am guessing here I have to put this is in an Action Helper. Just wanted to check what options I have and what are the best practices.
Thanks for any suggestions.
for that purposes you may use Zend_Registry singleton class for providing accessibility of session data from every point of ZF-project. For storing session data there are also several classes, e.g. Zend_Session class
I would definitely put this in a Controller Plugin class. eg.
Application_Controllerplugins_SessConfig extends Zend_Controller_Plugin_Abstract
This way you can catch the request early on, and do whatever you want with the data...
This is another answer of mine on SSL routing, but the logic is the same for the plugin class: How to implement SSL in Zend MVC
i am about to implement a similar solution whereby an application instance (which is to be rolled out to various customers) needs certain settings-data to be editable by admin via cms and thus am storing it in the db. i am then going to retrieve and add these settings [as a sub-object] to the config class (which in turn is added to the registry in the bootstrap. this seems like the cleanest solution to me as then all config data are stored in the one place, regardless of their source being an ini file or the db.
i'd be interested to hear other opinions on how this is done.
rob ganly
I'm trying to separate my mongodb code in a nodejs express app and am having troubles understanding how to shuffle things around.
here's a gist of what I have
https://gist.github.com/759446
I've dumbed it down to almost nothing in the middleware.
When I start the server, res.myvar is correctly set to "object" in the first request. Every subsequent request fails with res.myvar being "undefined".
I just want to stick the db code in a single place and then be able to use it in my various routes. Am I going about this wrong? All the examples on the mongodb nodejs driver page just make db queries etc. I'm not finding much integrating the whole thing in an express app.
What I had originally was opening new db connections on every request.
I've edited the gist and left the old stuff commented out.
It works now.
Many thanks to Ciaran's blog post http://howtonode.org/express-mongodb
It's kinda old but still helped.
I am writing node-fourm, I have the same problem at beginning. There is a db folder and controller folder in node-fourm, they are seperated now, and I can define method for each collection now. Check the code for detail.
I wrote mongoskin to make it possible.