为什么Doctrine MongoDB ODM总是返回NULL


Why does Doctrine MongoDB ODM always return NULL?

我在新安装时遇到了问题

    /**
     * @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
    }

我不能读取数据,我知道是在数据库。我也不能保存数据(即使我可以得到新生成的ID)

问题部分解决。

更改这一行:./vendor/Doctrine - MongoDB/lib/Doctrine/MongoDB/Collection.php #146

--return $this->mongoCollection->batchInsert($a, $options);
++return $this->mongoCollection->batchInsert($a);

抛出一个警告(batchInsert只期望1个参数,2给出9),该警告将阻止文档被保存。用@抑制警告没有帮助。现在的问题是,$options参数是安全写入所需要的,我不知道如何解决这个问题。

您需要升级Mongo扩展。PHP文档声明第二个参数是在v1.0.5中添加的。

http://us3.php.net/manual/en/mongocollection.batchinsert.php