Reputation: 85
I'm playin' around with zend framework 1.11 and mongo. I've decided to use Shanty_Mongo as a library to easy couple Zend and Mongo, but I'm stuck in this exception:
Can not save documet. Document is not connected to a db and collection
This is the code in the controller:
public function indexAction()
{
try {
$guestbook = new Application_Model_Guestbook();
$guestbook->setComment('Commento di prova')
->setEmail('[email protected]')
->save();
$all_elements = Application_Model_Guestbook::all();
$this->view->entries = $all_elements;
} catch (Exception $exc) {
echo $exc->getMessage();
}
}
This is (part) of the model:
class Application_Model_Guestbook extends Shanty_Mongo_Document
{
protected static $_db = 'test';
protected static $_collection = 'user';
protected $_comment;
.....
Shanty is in my library folder, and in application.ini i've added it:
resources.view[] =
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
autoloaderNamespaces[] = "Shanty"
On Shanty-Mongo docs, it's reported that
"If you are connecting to localhost without any authentication then no need to worry about connections any further. Shanty Mongo will connect automatically on the first request if no connections have previously been added."
but this does not happen.. I really can't guess why. Obviously, mongo is running, since if i use php Mongo() i can access it and perform insertions, etc...
I'm running the latest version of mongo, zend on php 5.3.6 on osx 10.6.8 Thanks!
Upvotes: 0
Views: 1546
Reputation: 5512
Try adding this to Bootstrap.php:
protected function _initMongoDB() {
$connection = new Shanty_Mongo_Connection('mongodb://localhost:27017');
Shanty_Mongo::addMaster($connection);
}
Upvotes: 0
Reputation: 155
Your model should be like this
class Application_Model_Guestbook extends Shanty_Mongo_Document
{
protected static $_db = 'test';
protected static $_collection = 'user';
protected static $_requirements = array('comment'=>'Required')
Upvotes: 1
Reputation: 2447
Allesio,
the element that both you and Adam C have put on the autoloaderNamespaces array are not quite correct. Try the following:
autoloaderNamespaces[] = "Shanty_"
You only need to put the top-level prefix followed by an underscore. Please let me know if this doesn't resolve the situation. Also, I've not seen that error message before. For sure, if you have a local install of mongoDB running, you won't need to specify any authentication parameters.
If the collection doesn't exist, Shanty will create it and if the document doesn't exist, Shanty will create that as well.
What operating system are you using?
I had a number of troubles with the package in the Ubuntu repositories. However, adding the 10gen repository in to apt and installing the latest stable version helped me out. Though even that seems to crash periodically.
Upvotes: 0
Reputation: 561
That's an odd error message. Notice it doesn't say "unable to connect to MongoDB" or similar. It says that this document isn't connected to a collection. It sounds like a configuration issue to me.
In other areas of your code are you able to connect to the database?
Read from the database?
Upvotes: 0
Reputation: 21692
I think you may want to switch that autoloaderNamespaces[] = "Shanty" line to be:
autoloaderNamespaces[] = 'Shanty_Mongo'
Other than that it looks OK....
Upvotes: 0