Why does Doctrine MongoDB ODM always return NULL?
-
10-03-2021 - |
Question
I'm having trouble making this work in a fresh install
/**
* @Route("/bla")
* @Template()
*/
public function blaAction()
{
$repository = $this->get('doctrine.odm.mongodb.document_manager')->getRepository('CompanySomeBundle:User');
$user = $repository->findOneByUsername('bla');
var_dump($user); // NULL
return new Response($user->getUsername()); // Fatal Error, user is not an object
}
/**
* @Route("/save-bla")
*/
public function saveBlaAction()
{
$user = new \Company\SomeBundle\Document\User;
$user->setUsername('bla');
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$dm->persist($user);
$dm->flush();
return new Response($user->getId()); // prints a new ID as expected, but nothing is actually saved to the DB
}
I can't read data that I know is in the DB. And I can't save data either (even though I can get the new generated ID)
Note: PHP's native Mongo works just fine.
Solution
Problem partially solved.
Changed this line: ./vendor/doctrine-mongodb/lib/Doctrine/MongoDB/Collection.php #146
--return $this->mongoCollection->batchInsert($a, $options);
++return $this->mongoCollection->batchInsert($a);
It was throwing a warning (batchInsert expects exactly 1 parameter, 2 given9 that would stop the documents from being saved. Suppressing the warning with a @ didn't help. The problem now is that the $options argument is needed for safe writes, and I don't know how to fix this.
OTHER TIPS
You need to upgrade your Mongo extension. The PHP docs state the second parameter was added in v1.0.5.
http://us3.php.net/manual/en/mongocollection.batchinsert.php