I was working on putting POPO to MongoDB in symfony project.
I have the Document inside BundleDocumentMyDocument.php
I want to create this document for the first time, but would like to update the document if it exists based on its two fields like:
* I have taken out some of additional information and focus on the main code only..
$file_id = getTheFileId(); $accessor_id = getAccessorId(); $repository=$this->getMongoService() ->getManager() ->getRepository('SomethinSomethingBundleDocumentFileAccess'); $file_access=$repository->findOneBy(array('fileId'=>$file_id, 'accessorId'=>$accessor_id)); if ($file_access && $file_access instanceof FileAccess){ $file_access->setAccessCount($file_access->getAccessCount()+1); }else{ $file_access=new FileAccess(); $file_access->setFileId($file_id); $file_access->setAccessorId($accessor_id); $file_access->setAccessorType(FileAccess::ACCESS_TYPE_MEMBER); $file_access->setAccessDate(time()); $file_access->setAccessCount(1); } $document_manager=$this->getMongoService()->getManager(); $document_manager->persist($file_access); $document_manager->flush();
The problem I was facing was when I try to access the record, not when I was adding it.
and I know the record was there by using the command line mongo tool
The solution appears to be the missing config item for mongo:
on your config for mongo add
auto_generate_hydrator_classes: true
and that should fix the problem.