简体   繁体   English

Symfony3 MongoDB服务错误

[英]Symfony3 MongoDB service error

I am trying to set up Symfony3 with MongoDB by following this tutorial: Symfony3 Docs 我正在通过遵循此教程来使用MongoDB设置Symfony3: Symfony3 Docs

I followed everything right through and after Persist Object to MongoDb Chapter i reloaded my page and get this error: 在Persist Object到MongoDb章之后,我一直遵循所有内容,重新加载了页面并得到此错误:

Error: Call to a member function get() on null

If i underst it right it means that the get method cannot find the declared service? 如果我理解正确,则意味着get方法找不到声明的服务?

my Controller code: 我的控制器代码:

    <?php

namespace Cambio\CambioBundle\Controller;

use Cambio\CambioBundle\Document\Product;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class AuthenticationController extends Controller
{
    public function loginAction()
    {
        $product = new Product();
        $product->setName('A Foo Bar');
        $product->setPrice('19.99');

        $dm = $this->get('doctrine_mongodb')->getManager();
        $dm->persist($product);
        $dm->flush();

        return new Response('Created product id '.$product->getId());
    }
}

and this is my config.yml: 这是我的config.yml:

# MongoDB Configuration
doctrine_mongodb:
    connections:
        default:
            server: mongodb://localhost:27017
            options: {}
    default_database: test_database
    document_managers:
        default:
            auto_mapping: true

Not quiet sure what is the problem and how i can fix it. 不安静地确定问题是什么以及如何解决。

Additional Errol Logs: 其他Errol日志:

in src/Cambio/CambioBundle/Controller/AuthenticationController.php at line 18   -
        $product->setPrice('19.99');
//        $m = $this->container->get('doctrine_mongodb.odm.default_connection');
        $dm = $this->container->get('doctrine_mongodb.odm.default_connection')->getManager();
        $dm->persist($product);
        $dm->flush();

and Log: 和日志:

CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalErrorException: "Error: Call to a member function get() on null" at /Users/Tomazi/Dev/SymfMongo/src/Cambio/CambioBundle/Controller/AuthenticationController.php line 18 

More edits: 更多编辑:

OK so i go this working previously my Controller was defined as service and this set up did not work as soon as i added new Controller and did not declare it as service every magically works: 好的,这样我就可以在以前将我的Controller定义为service的情况下进行此工作,并且在我添加新Controller并没有将其声明为service时,该设置就无法正常工作了:

Routing.yml: Routing.yml:

    cambio_test:
    path:     /test
    defaults: { _controller: CambioBundle:Default:index }

cambio_homepage:
    path:     /
    defaults: { _controller: cambio.authentication.controller:loginAction }

Services.xml: Services.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>

        <!-- Controller -->
        <service id="cambio.authentication.controller"
                 class="Cambio\CambioBundle\Controller\AuthenticationController">
        </service>

    </services>
</container>

and the Default controller has the same code init it any idea why it does not work when Controllers is decleared as a service but it does work when controller isynt defined as one...? 并且Default控制器具有相同的代码init,无论为什么将Controllers取消为服务时,它都不起作用,但是当将isynt控制器定义为one ...时,它确实起作用。

Most likely, you are using wrong service name. 您很可能使用了错误的服务名称。 In the linked documentation they are using something else: 链接的文档中,他们使用其他方式:

$m = $this->get('doctrine_mongodb.odm.default_connection');

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

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