简体   繁体   English

为什么Doctrine MongoDB ODM总是返回NULL?

[英]Why does Doctrine MongoDB ODM always return NULL?

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) 而且我也无法保存数据(即使我可以获取新生成的ID)

Note: PHP's native Mongo works just fine. 注意:PHP的本机Mongo可以正常工作。

Problem partially solved. 问题已部分解决。

Changed this line: ./vendor/doctrine-mongodb/lib/Doctrine/MongoDB/Collection.php #146 更改此行:./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. 它发出了警告(batchInsert期望恰好有1个参数,2个给定的9参数,这将阻止文档的保存。用@禁止警告没有帮助。现在的问题是,安全写入需要使用$ options参数,并且我不知道该如何解决。

You need to upgrade your Mongo extension. 您需要升级您的Mongo扩展程序。 The PHP docs state the second parameter was added in v1.0.5. PHP文档指出在v1.0.5中添加了第二个参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM